-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImplementStr.java
More file actions
28 lines (25 loc) · 875 Bytes
/
ImplementStr.java
File metadata and controls
28 lines (25 loc) · 875 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
public class ImplementStr {
// "mississippi","issipi"
public static int strStr(String haystack, String needle) {
if (haystack.length() < needle.length()) return -1;
if (haystack != null && haystack.equals(needle)) return 0;
for (int i = 0; i < haystack.length(); i++) {
final int index = i;
int count = 0;
for (int j = 0; j < needle.length() && (i + j ) < haystack.length(); j++) {
if (haystack.charAt(i + j) == needle.charAt(j)){
count++;
}else{
break;
}
}
if (count == needle.length()){
return index;
}
}
return -1;
}
public static void main(String[] args) {
System.out.println(strStr("",""));
}
}