forked from team-codebug/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path28ImplementStrStr.java
More file actions
37 lines (28 loc) · 836 Bytes
/
28ImplementStrStr.java
File metadata and controls
37 lines (28 loc) · 836 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
30
31
32
33
34
35
36
37
class Solution {
public int strStr(String haystack, String needle) {
if (needle.length() == 0) {
return 0;
}
if (needle.length() > haystack.length()) {
return -1;
}
int i = 0;
while (i < haystack.length()) {
if (i + needle.length() > haystack.length()) {
break;
}
int j = 0;
while (j < needle.length()) {
if (needle.charAt(j) != haystack.charAt(i + j)) {
break;
}
j += 1;
}
if (j == needle.length()) {
return i;
}
i += 1;
}
return -1;
}
}