c++ Runtime: 6 ms


  • 0
    Y

    class Solution {
    public:
    int strStr(string haystack, string needle) {
    int i = 0;
    int j = 0;
    while(i < haystack.length() && j < needle.length()){
    if(haystack[i] == needle[j]){
    i++;
    j++;
    }
    else{
    i = i-j+1;
    j = 0;
    }
    }
    if(j == needle.length()&& i<haystack.length()) return i-j;
    else if(j == needle.length()&&i == haystack.length()&&needle[j]==haystack[i]) return i-j;
    else return -1;
    }
    };


Log in to reply
 

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