My simple c++ solution


  • -1
    K
    class Solution {
    public:
    int strStr(string haystack, string needle) {
    
        if(haystack.empty()&&needle.empty()||haystack==needle)return 0;
        if(haystack.empty()&&!needle.empty()||haystack.length()<needle.length())return -1;
        for(int i=0;i<=haystack.length()-needle.length();i++)if(haystack.substr(i,needle.length())==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.