ダブルポインタを使った暴力解決力ボタン28題「strStr()の実現」

530 ワード

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

時間複雑度$O(M*N)$