Where is the bug?

  • 0

    This is the right code.

        bool isScramble(string s1, string s2) {
        if (s1==s2) return 1;
        if (s1.length()!=s2.length()) return 0;
        string rs2(s2);
        reverse(rs2.begin(), rs2.end());
        string cps1(s1), cps2(s2);
        sort(cps1.begin(), cps1.end());
        sort(cps2.begin(), cps2.end());
        if (cps1!=cps2) return 0;
        int strlen = s1.length();
        for(int id=1; id<strlen; id++) {
            if (isScramble(s1.substr(0, id), s2.substr(0, id)) &&
                isScramble(s1.substr(id), s2.substr(id)))
                return 1;
            if (isScramble(s1.substr(0, id), rs2.substr(0, id)) &&
                isScramble(s1.substr(id), rs2.substr(id)))
                return 1;
        return 0;

    Before I got the right code, I made one mistake,

    id<strlen -> id<strlen-1,

    so i always got time limit exceeds error.
    I reduce the number of iterations, why got time limit error?

Log in to reply

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