clueless coding // TODO: be smarter

LeetCode Implement strStr(): CPP Solution



TL;DR Code Solution


int strStr(string haystack, string needle) {

    int n = haystack.length();
    int m = needle.length();

    if (!m) {
        return 0;
    }

    for (int i = 0; i < n - m + 1; i++) {
        for (int j = 0; j < m; j++) {
            if (haystack[i + j] != needle[j]) break;
            if (j == needle.length() - 1) return i;   
        }
    }
    return -1;
}