forked from derekhh/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindrome-index.cpp
More file actions
42 lines (39 loc) · 756 Bytes
/
Copy pathpalindrome-index.cpp
File metadata and controls
42 lines (39 loc) · 756 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
//palindrome-index.cpp
//Palindrome Index
//Weekly Challenges - Week 4
//Author: derekhh
#include<iostream>
#include<string>
using namespace std;
bool ispalin(string str)
{
int i = 0, j = str.length() - 1;
while (i < j)
{
if (str[i] != str[j]) return false;
i++, j--;
}
return true;
}
int main()
{
int t;
cin >> t;
while (t--)
{
string str;
cin >> str;
int i = 0, len = str.length(), j = len - 1;
int ans = -1;
while (i < j && str[i] == str[j]) i++, j--;
if (i < j)
{
string str1 = str.substr(0, i) + str.substr(i + 1, str.length() - i - 1);
if (ispalin(str1)) ans = i;
string str2 = str.substr(0, j) + str.substr(j + 1, str.length() - j - 1);
if (ispalin(str2)) ans = j;
}
cout << ans << endl;
}
return 0;
}