-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD15_min_char_palindrome.cpp
More file actions
66 lines (49 loc) · 1.76 KB
/
D15_min_char_palindrome.cpp
File metadata and controls
66 lines (49 loc) · 1.76 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
// Given a string s, the task is to find the minimum characters to be added at the front to make the string palindrome.
// Note: A palindrome string is a sequence of characters that reads the same forward and backward.
// Examples:
// Input: s = "abc"
// Output: 2
// Explanation: Add 'b' and 'c' at front of above string to make it palindrome : "cbabc"
// Input: s = "aacecaaaa"
// Output: 2
// Explanation: Add 2 a's at front of above string to make it palindrome : "aaaacecaaaa"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
int minChar(string& s) {
// Reverse the string to check the matching prefix
string rev_s = s;
reverse(rev_s.begin(), rev_s.end());
// Concatenate the string with a separator
string concat = s + "#" + rev_s;
// Compute the LPS (Longest Prefix Suffix) array
vector<int> lps(concat.size(), 0);
int j = 0; // length of the previous longest prefix suffix
// Fill the LPS array
for (int i = 1; i < concat.size(); i++) {
while (j > 0 && concat[i] != concat[j]) {
j = lps[j - 1];
}
if (concat[i] == concat[j]) {
j++;
}
lps[i] = j;
}
// The number of characters to add to the front is:
// length of s - LPS value at the end of the concatenated string
return s.size() - lps[concat.size() - 1];
}
};
int main() {
Solution sol;
// Test case 1
string s1 = "abc";
cout << sol.minChar(s1) << endl; // Output: 2
// Test case 2
string s2 = "aacecaaaa";
cout << sol.minChar(s2) << endl; // Output: 2
return 0;
}