Simple and easy to understand c++ solution


  • 0
    C

    int strStr(string haystack, string needle) {
    if (needle.size()==0) return 0;
    if (haystack.size()==0) return -1;
    int cur=0, last=haystack.size()-needle.size();
    bool found = false;
    while(cur<=last && !found){
    found = true;
    for (int i=0; i<needle.size();i++){
    if (haystack[cur+i]!=needle[i]){
    cur++;
    found=false;
    break;
    }
    }
    }
    return (found?cur:-1);
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.