28. Find the Index of the First Occurrence in a String

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