- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem28.java
More file actions
Latest commit
26 lines (25 loc) · 721 Bytes
/
Copy pathProblem28.java
File metadata and controls
26 lines (25 loc) · 721 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
classProblem28 {
publicstaticvoidmain(String[] args) {
Problem28p = newProblem28();
Stringhaystack = "mississippi";
Stringneedle = "issip";
System.out.println(p.strStr(haystack, needle));
}
publicintstrStr(Stringhaystack, Stringneedle) {
if (needle.length() <= 0) {
return -1;
}
for (inti = 0, j = 0; i < haystack.length(); i++) {
if (haystack.charAt(i) == needle.charAt(j)) {
j++;
if (j == needle.length()) {
returni-j+1;
}
} else {
i = i - j;
j = 0;
}
}
return -1;
}
}