Further concise Sunday algorithm in c++


  • 0
    X
    class Solution {
    public:
        int strStr(string src, string dst)
        {
            int len1=src.size(),len2=dst.size(),pos=0;
            while(len2+pos<=len1)
            {
                int count=0;
                while(count<len2&&dst[count]==src[count+pos])
                    count++;
                if(count==len2)
                    return pos;
                int rightNext=pos+len2,shift=1;
                for(shift=1;shift<=len2;shift++)
                    if(src[rightNext]==dst[len2-shift])
                        break;
                pos+=shift;
            }
            return -1;
        }
    };
    

Log in to reply
 

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