Implement strStr()
Description
Clarification
Example
Challenge
public class Solution {
/**
* @param source:
* @param target:
* @return: return the index
*/
public int strStr(String source, String target) {
// Write your code here
if(target == null || source == null) return -1;
if(target.length() == 0){
return 0;
}
if( source.length() < target.length()){
return -1;
}
for (int i = 0; i <= source.length() - target.length();i++ ){
int j = 0;
for(j = 0; j < target.length();j++ ){
if(target.charAt(j) != source.charAt(i+j)){
break;
}
}
if(j == target.length()){
return i;
}
}
return -1;
}
}Last updated