Failed on test "ab"&&"ab" is true


  • 0
    P

    Hello!
    My code failed on input s1 = ab, s2 = ab, expect true.
    Is it right? I think that s2 must be "ba" to be right.

        bool checkInclusion(string s1, string s2) {
            short int s_length = s1.length();
            if(s_length > s2.length()) return false;
            s1 = s1 + "A" + s2;
            short int values[s1.length()];
            for(short int i=0; i<s1.length(); i++) values[i] = s_length;
            for(short int i = s_length+1; i < s1.length(); i++) {
                short int idx = values[i-1];
                while(idx<s_length && s1[i] != s1[idx-1])
                    idx = values[idx+1];
                if(s1[i] == s1[idx-1]) --idx;
                if(!idx) return true;
                values[i] = idx;
            }
            return false;    
        }
    

Log in to reply
 

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