c++ | easy | fast
\n\n# Code\n\n//from mehuljain53\nclass Solution {\npublic:\n    int palindromePartition(string s, int K) {\n        vector<vector<int>> dp(101,vector<int>(101,
\n\n# Code\n```\n//from mehuljain53\nclass Solution {\npublic:\n    int palindromePartition(string s, int K) {\n        vector<vector<int>> dp(101,vector<int>(101,100));\n        vector<vector<int>> pd(101,vector<int>(101,101));\n\t\t\n        for(int i = 0; i < s.size(); i++) {\n            pd[i][i] = 0;\n        }\n        for(int i = 0; i < s.size()-1;i++) {\n            if(s[i] == s[i+1])\n                pd[i][i+1] = 0;\n            else\n                pd[i][i+1] = 1;\n        }\n        \n        for(int k = 2; k < s.size(); k++) {\n            for(int i = 0; i < s.size(); i++) {\n                if(i+k>=s.size())\n                    continue;\n                int j = i + k;\n                pd[i][j] = pd[i+1][j-1];\n                if(s[i]!=s[j])\n                    pd[i][j] = pd[i+1][j-1] + 1;\n            }\n        }\n        \n        for(int j = 0; j < s.size(); j++) {\n            dp[j][1] = pd[0][j];\n        }\n        \n        for(int k = 2;  k <= K; k++ ) {\n            for(int j = 1; j < s.size(); j++) {\n                dp[j][k] = j+1;\n                for(int l = 0; l <= j; l++) {\n                    dp[j][k] = min(dp[l][k-1] + pd[l+1][j], dp[j][k]);\n                }\n            }\n        }\n        return dp[s.size()-1][K];\n        \n    }\n};\n```
['C++']