-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathImplement strStr().java
More file actions
38 lines (35 loc) · 1.11 KB
/
Implement strStr().java
File metadata and controls
38 lines (35 loc) · 1.11 KB
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
38
/*
Implement strStr().
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
*/
public class Solution {
public String strStr(String haystack, String needle) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
int length1 = haystack.length();
int length2 = needle.length();
if (length2 == 0) {
return haystack;
}
if (length2 > length1) {
return null;
}
for (int i = 0; i + length2 <= length1; i++) {
if (haystack.charAt(i) != needle.charAt(0)) {
continue;
}
int k = i + 1;
for (int j = 1; j < length2 && k < length1; j++) {
if (needle.charAt(j) == haystack.charAt(k)) {
k++;
} else {
break;
}
}
if (k == i + length2) {
return haystack.substring(i);
}
}
return null;
}
}