"Implement strStr()" Simple Solution in C++


  • 0
    M
    class Solution {
    public:
        int strStr(string haystack, string needle) {
            for (int i = 0; i <= (int)haystack.size() - (int)needle.size(); ++i) {
                size_t j = 0;
                for (j = 0; j < needle.size(); ++j) {
                    if (haystack[i + j] != needle[j]) break;
                }
                if (j == needle.size()) return i;
            }
            return -1;
        }
    };
    

Log in to reply
 

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