Minimum Insertion Steps to Make a String Palindrome LeetCode solution in C++

1
class Solution {
2
public:
3
int t[501][501];
4
int MinInsertion(string x, int m) {
5
string y = x;
6
reverse(y.begin(), y.end());
7
for (int i = 0; i < m + 1; i++) {
8
for (int j = 0; j < m + 1; j++) {
9
if (i == 0 || j == 0) t[i][j] = 0;
10
}
11
}
12
for (int i = 1; i < m + 1; i++) {
13
for (int j = 1; j < m + 1; j++) {
14
if (x[i - 1] == y[j - 1])
15
t[i][j] = 1 + t[i - 1][j - 1];
16
else
17
t[i][j] = max(t[i - 1][j], t[i][j - 1]);
18
}
19
}
20
return m - t[m][m];
21
}
22
int minInsertions(string s) {
23
return MinInsertion(s, s.length());
24
}
25
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0