Why my solution got wrong answer


  • 0
    J
    int minCut(string s) {
        // write your code here
        if (s.length() <= 1) {
            return 0;
        }
        int n = s.length();
        vector<int> cuts(n, 0);
        bool isP[n][n];
        
        for (int i = 0; i < n; ++i) {
            cuts[i] = i;
            for (int j = 0; j <= i; ++j) {
                if (s[i] == s[j] && (i - j <= 1 || isP[j + 1][i - 1]) ) {
                    isP[j][i] = true;
                    cuts[i] = j == 0 ? 0 : min(cuts[i], cuts[j - 1] + 1);
                }
            }
        }
        return cuts[n - 1];
    }
    

    input: cabababcbc

    output: 2

    expected: 3

    Thanks in advance!

    Edit:
    initialized isP[n][n] make it work. Is it due to different compilers' different default bool value?
    Any thoughts please comment.


Log in to reply
 

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