-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path28.java
29 lines (20 loc) · 835 Bytes
/
28.java
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
class Solution {
//For some internet connection problem, result is not true obviously.
public static int strStr(String haystack, String needle) {
final int hayLength = haystack.length();
final int needleLength = needle.length();
if(hayLength < needleLength)
return -1;
final char needleChar = needle.charAt(0);
for(int i = 0; i < hayLength; i++){
final boolean isExceed = i + needleLength > hayLength;
if(isExceed) break;
final char haysChar = haystack.charAt(i);
if(haysChar != needleChar) continue;
final String substring = haystack.substring(i, i + needleLength);
final boolean equals = substring.equals(needle);
if(equals) return i;
}
return -1;
}
}