Leetcode#28 Implement strStr()

less than 1 minute read

Published:

https://leetcode.com/problems/implement-strstr/description/

Solution:

class Solution {
public:
    int strStr(string haystack, string needle) {
        if (needle.size() == 0)
            return 0;

        if(needle.size() > haystack.size())
            return -1;

        if (haystack.size() == 0)
            return -1;

        for(int i=0; i<=haystack.size()-needle.size(); i++) {
            bool match = true;
            for(int j=0; j<needle.size();j++){
                if(haystack[i+j] != needle[j]){
                    match = false;
                    break;
                }   
            }
            if (match)
                return i;
        }
        return -1;
    }
};