public class Solution {
/*
* @param source: source string to be scanned.
* @param target: target string containing the sequence of characters to match
* @return: a index to the first occurrence of target in source, or -1 if target is not part of source.
*/
public int strStr(String source, String target) {
if(source==null||target==null){
return -1;
}
if(target.length()==0){
return 0;
}
char[] sou=source.toCharArray();
char[] tag=target.toCharArray();
for(int i=0;i<=sou.length-tag.length;i++){
if(sou[i]==tag[0]){
for(int k=0;k<=tag.length;k++){
if(k==tag.length){
return i;
}
if(!(sou[k+i]==tag[k])){
break;
}
}
}
}
return -1;
}
}
/*
* @param source: source string to be scanned.
* @param target: target string containing the sequence of characters to match
* @return: a index to the first occurrence of target in source, or -1 if target is not part of source.
*/
public int strStr(String source, String target) {
if(source==null||target==null){
return -1;
}
if(target.length()==0){
return 0;
}
char[] sou=source.toCharArray();
char[] tag=target.toCharArray();
for(int i=0;i<=sou.length-tag.length;i++){
if(sou[i]==tag[0]){
for(int k=0;k<=tag.length;k++){
if(k==tag.length){
return i;
}
if(!(sou[k+i]==tag[k])){
break;
}
}
}
}
return -1;
}
}