-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathE0073.cpp
More file actions
46 lines (38 loc) · 772 Bytes
/
E0073.cpp
File metadata and controls
46 lines (38 loc) · 772 Bytes
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
/*
Problem Statement: https://www.hackerrank.com/challenges/palindrome-index/problem
*/
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool isPalindrome(string s) {
return equal(s.begin(), s.begin() + s.length() / 2, s.rbegin());
}
int palindromeIndex(string s) {
int i, j;
string s1, s2;
if (isPalindrome(s))
return -1;
for (i = 0, j = s.length() - 1; i < s.length() / 2; i++, j--)
if (s[i] != s[j]) {
s1 = s.substr(0, i) + s.substr(i + 1);
s2 = s.substr(0, j) + s.substr(j + 1);
break;
}
if (isPalindrome(s1))
return i;
else if (isPalindrome(s2))
return j;
else
return -1;
}
int main() {
int q;
cin >> q;
while (q--) {
string s;
cin >> s;
cout << palindromeIndex(s) << endl;
}
return 0;
}