See the original problem on HackerRank.
If the string is already a palindrome, -1 is the answer since no character need be removed.
If the given string is not a palindrome, we must find one character that, once removed, will make it a palindrome. We can do this by checking if
str[i] == str[N - 1 - i] where
N is the length of the string for all
i starting from
i=0. Once this condition fails, all we have to do is to check if
str[0:i-1] + str[i+1:N-1] is a palindrome. If it is a palindrome, we print the value of
i; otherwise, we print the value of
Possible C++ implementation: