-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalind.c
53 lines (43 loc) · 1.01 KB
/
palind.c
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
//{ Driver Code Starts
/* Driver program to test above function */
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
int minChar(string& s) {
// Write your code here
string rev_s = s;
reverse(rev_s.begin(), rev_s.end());
string concat = s + "#" + rev_s;
int n = concat.length();
vector<int> lps(n, 0);
for (int i = 1; i < n; i++) {
int j = lps[i - 1];
while (j > 0 && concat[i] != concat[j]) {
j = lps[j - 1];
}
if (concat[i] == concat[j]) {
j++;
}
lps[i] = j;
}
return s.length() - lps[n - 1];
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
string str;
cin >> str;
Solution ob;
int ans = ob.minChar(str);
cout << ans << endl;
cout << "~"
<< "\n";
}
return 0;
}
// } Driver Code Ends