- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplementStrStr_28.java
More file actions
Latest commit
21 lines (19 loc) · 715 Bytes
/
Copy pathImplementStrStr_28.java
File metadata and controls
21 lines (19 loc) · 715 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* 1. the brute force is very straightforward
* - O(mn)
* - for(int i=0, j; i<haystack.length(); i++) will cause the length function to be called again & again
* will casue Time Limit Exceeded
*/
publicclassSolution {
publicintstrStr(Stringhaystack, Stringneedle) {
inthaystacklength = haystack.length();
intneedlelength = needle.length();
if (needlelength == 0) return0; // XXXX
for(inti=0, j; i<haystacklength; i++) {
for(j=0; j<needlelength && i+j < haystacklength
&& needle.charAt(j) == haystack.charAt(i+j); j++);
if (j==needle.length()) returni;
}
return -1;
}
}