Using substr() | c++ | 4ms


  • -2
    D
    class Solution {
    public:
        int strStr(string haystack, string needle) {
            int ln = needle.length();
            int lh = haystack.length();
            if(!ln) return 0;
            if(!lh) return -1;
            
            string c="";
            for(int i=0; i<=lh-ln;i++){
                c = haystack.substr(i,ln);
                if(c==needle) return i;
            }
            
            return -1;
        }
    };

Log in to reply
 

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