-
Notifications
You must be signed in to change notification settings - Fork 110
/
solution.cpp
61 lines (45 loc) · 1.36 KB
/
solution.cpp
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
#include <bits/stdc++.h>
using namespace std;
// check palindrome of string from start to end
bool isPalindrome(string s, int start, int end) {
for (int i = start, j = end; i < j; i++, j--) {
if (s[i] != s[j])
return false;
}
return true;
}
// Implementation Part
int palindromeIndex(string s) {
// traverse from start and back
for (int i = 0, j = s.length()-1; i < j; i++, j--) {
// check when ith and corresponding jth character are not same
if (s[i] != s[j]) {
// skip i th character and check again if it palindrome then,
// remvve ith letter from start
if (isPalindrome(s, i+1, j))
return i;
// skip jth character and check is it palindrome, if it is then,
// remove jth letter from back
if (isPalindrome(s, i, j-1))
return j;
// if it is palindrome then no need to remove anything
return -1;
}
}
return -1;
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
int q;
cin >> q;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
for (int q_itr = 0; q_itr < q; q_itr++) {
string s;
getline(cin, s);
int result = palindromeIndex(s);
fout << result << "\n";
}
fout.close();
return 0;
}