-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path32_MinInsetion.cpp
More file actions
93 lines (82 loc) · 2.46 KB
/
Copy path32_MinInsetion.cpp
File metadata and controls
93 lines (82 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// https://leetcode.com/problems/minimum-insertion-steps-to-make-a-string-palindrome/
// Given a string s. In one step you can insert any character at any
// index of the string.
// Return the minimum number of steps to make s palindrome.
// A Palindrome String is one that reads the same backward as well as forward.
#include <bits/stdc++.h>
using namespace std ;
class Solution3
{
// Using Longest Palindromic Subsequence
public:
int longestPalindromeSubseq(string s)
{
// We just need to find the longest Palindromic subsequence
// in string s and rev(s);
// Since we can insert anywhere in the string: Subseq
string t = s;
reverse(s.begin(), s.end());
int n = s.size();
vector<int> cp(n + 1), xp(n + 1);
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= n; ++j)
{
if (s[i - 1] == t[j - 1]) xp[j] = 1 + cp[j - 1];
else xp[j] = max(xp[j - 1], cp[j]);
}
cp = xp;
}
return (n - cp[n]);
}
};
class Solution2 {
// Tabulation
public:
int minInsertions(string s) {
int n = s.length();
vector<vector<int>> dp(n, vector<int>(n));
int x = 1;
while (x < n)
{
for (int i = 0, j = x; i < n && j < n; ++i, ++j)
if (s[i] == s[j]) dp[i][j] = dp[i + 1][j - 1];
else dp[i][j] = 1 + min(dp[i + 1][j], dp[i][j - 1]);
++x ;
}
return dp[0][n - 1];
}
};
class Solution1 {
// Recursion: Memoization
public:
int minInsertions(string s) {
int n = s.length();
vector<vector<int>> dp(n, vector<int>(n, -1));
return solve(s, dp, 0, n-1);
}
int solve(string &s, vector<vector<int>> &dp, int i, int j) {
if (i >= j) return 0 ;
if (dp[i][j] != -1) return dp[i][j];
if (s[i] == s[j]) return dp[i][j] = solve(s, dp, i+1, j-1);
else return dp[i][j] = 1 + min(solve(s, dp, i+1, j), solve(s, dp, i, j-1));
}
};
class Solution {
// BruteForce: Recursion
public:
int minInsertions(string s) {
int n = s.length();
return solve(s, 0, n-1);
}
int solve(string &s, int i, int j) {
if (i >= j) return 0 ;
if (s[i] == s[j]) return solve(s, i+1, j-1);
else return 1 + min(solve(s, i+1, j), solve(s, i, j-1));
}
};
int main () {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}