forked from Jensenczx/CodeEveryday
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path108_strStr.java
More file actions
26 lines (26 loc) · 800 Bytes
/
108_strStr.java
File metadata and controls
26 lines (26 loc) · 800 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
class Solution {
/**
* Returns a index to the first occurrence of target in source,
* or -1 if target is not part of source.
* @param source string to be scanned.
* @param target string containing the sequence of characters to match.
*/
public int strStr(String source, String target) {
//write your code here
if(source==null||target==null)
return -1;
int len1 = source.length();
int len2 = target.length();
if(len2==0)
return 0;
for(int i=0; i<=len1-len2; i++){
for(int j=0; j<len2; j++){
if(source.charAt(i+j)!=target.charAt(j))
break;
if(j==len2-1)
return i;
}
}
return -1;
}
}