Effective Solution


#1
int Solution::strStr(const string a, const string b) {
if(a.length() == 0 || b.length() == 0) return -1;
if(a.length() < b.length()) return -1;
int j = -1;
int count = 0;
int ans = -1;
for(int i=0;i<a.length();i++){
    if(a[i] == b[(j+1)]){
        j++;
        count++;
    }else if(j!=-1){
        if(count == b.length()){
            ans = i - count;
            break;
        }else{
            j=-1;
            i = i - count;
            count=0;
        }
    }
    
}
if(ans == -1){
    if(count == b.length()){
        ans = a.length() - b.length();
    }
}
return ans;

}