- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplementedStrStr.java
More file actions
Latest commit
29 lines (24 loc) · 918 Bytes
/
Copy pathImplementedStrStr.java
File metadata and controls
29 lines (24 loc) · 918 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
publicclassImplementedStrStr {
//With recursion:
publicintstrStr(Stringhaystack, Stringneedle) {
if (needle == null || needle == "") return0;
if (haystack == null) return -1;
char[] cArr1 = haystack.toCharArray();
char[] cArr2 = needle.toCharArray();
for (inti = 0; i < cArr1.length; i++) {
if (subString(cArr1, cArr2, i, 0)) returni;
}
return -1;
}
privatebooleansubString(char[] cArr1, char[] cArr2, intstart, intindex) {
if (index == cArr2.length) {
returntrue;
}
if (start + index >= cArr1.length || cArr1[start + index] != cArr2[index]) returnfalse;
returnsubString(cArr1, cArr2, start, index + 1);
}
publicstaticvoidmain(String[] args) {
ImplementedStrStrs = newImplementedStrStr();
System.out.println(s.strStr("asd", "sd"));
}
}