A simple 4ms C++ Solution


  • -2
    Y
    class Solution {
    public:
        int strStr(string haystack, string needle) {
            int lenh = haystack.length();
            int lenn = needle.length();
            bool flag = true;
            if(needle == "") return 0;
            for(int i = 0; i < lenh - lenn + 1; i++) {
                if(haystack[i] == needle[0]) {
                    for(int j = 0; j < lenn; j++) {
                        if(haystack[j + i] != needle[j]) {
                            flag = false;
                            break;
                        } else flag = true;
                    }
                    if(flag) return i;
                }
            }
            return -1;
        }
    };

Log in to reply
 

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