-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_28_implement_strStr.java
More file actions
32 lines (29 loc) · 910 Bytes
/
Copy path_28_implement_strStr.java
File metadata and controls
32 lines (29 loc) · 910 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
public class _28_implement_strStr {
public static int strStr(String haystack, String needle) {
if (needle == "" || needle.isEmpty()) {
return 0;
}
char[] a = haystack.toCharArray();
char[] b = needle.toCharArray();
for (int i = 0; i < a.length; i++) {
if (a[i] == b[0]) {
int count = 1;
for (int j = 1; j < b.length; j++) {
if (i + j < a.length && a[i+j] == b[j]) {
count++;
} else {
break;
}
}
if (count == b.length) {
return i;
}
}
}
return -1;
}
public static void main(String[] args) {
String haystack = "", needle = "";
System.out.println(strStr(haystack, needle));
}
}