-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path28 First Occurence.java
44 lines (43 loc) · 1.29 KB
/
28 First Occurence.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Solution {
public int strStr(String haystack, String needle) {
char nStart = needle.charAt(0);
if(haystack.length()==1){
if(haystack.charAt(0) == needle.charAt(0)){
return 0;
}
else{
return -1;
}
}
else{
for(int i=0; i<(haystack.length()-needle.length()+1); i++){
if(haystack.charAt(i) == nStart){
//check
int temp = i;
boolean checker = true;
for(int j=0; j<needle.length(); j++){
if(haystack.charAt(temp) == needle.charAt(j)){
checker = true;
temp++;
}
else{
checker = false;
break;
}
}
if(checker == true){
return i;
}
else{
//break;
}
}
else{
//loop again
}
}
//default return if needle not found
return -1;
}
}
}