Simple C++ 2 lines solution


  • 0
    P
        class Solution {
    public:
        int strStr(string haystack, string needle) {
            
            int n1 = haystack.length();
            int n2 = needle.length();
            if(n2 > n1){
                return -1;
            }
            else if(n2==0){
                return 0;
            } 
            size_t found = haystack.find(needle);
            if(found != string::npos){
                return found;
            }
            else{
                return -1;
            }
        }
    };
    

Log in to reply
 

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