Skip to content

Commit 96e3276

Browse files
authored
Create 028. Implement strStr().java
1 parent d120564 commit 96e3276

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

028. Implement strStr().java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public class Solution {
2+
public int strStr(String haystack, String needle) {
3+
if (needle.length() == 0) return 0;
4+
5+
int h = 0;
6+
while (h <= haystack.length() - needle.length()) {
7+
if (haystack.charAt(h) == needle.charAt(0)) {
8+
boolean match = true;
9+
for (int n = 0, hP = h; n < needle.length(); n++, hP++) {
10+
if (haystack.charAt(hP) != needle.charAt(n)) {
11+
match = false;
12+
break;
13+
}
14+
}
15+
if (match == true) {
16+
return h;
17+
}
18+
}
19+
h++;
20+
}
21+
22+
return -1;
23+
}
24+
}

0 commit comments

Comments
 (0)