We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
There are two kinds of strings you will get in this problem.
1) One that is already palindrome.
2) One that is not palindrome, and will require you to remove one and only one character to make it palindrome.
We'll use the same method we use to check if a string is palindrome or not (i.e., check the first with the last character, check 2nd with 2nd last character, and so on). Except here, we note where we get an inequal condition. And after that we check if removing either of the two makes palindrome or not.
-
To understand, consider the following example.
aabcdcba
Clearly we need to remove the second 'a' to make it palindrome. But how will you make a program know that?
-
Lets now follow the method we described. First we check the first character with the last. Since both are a's, we move on. Second character 'a' and 2nd last 'b' dont match, so the string is not palindrome. Since removing only one character makes the string palindrome(as given in question), the character to be removed is one of these two characters. This is guaranteed to work for all strings, due to the constraints given in the question. Now you can remove the characters individually to check if string becomes palindrome, or simply observe that the characters after 'a' match with the corresponding characters starting back from 'b' on the other side, i.e.,
bcdcb
is palindrome. Both ways yield the same result.
-
If you don't get an inequality condition, the string is palindrome then. This problem is hence solved in linear time.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Palindrome Index
You are viewing a single comment's thread. Return to all comments →
I'll try explaining my way.
-
There are two kinds of strings you will get in this problem.
1) One that is already palindrome.
2) One that is not palindrome, and will require you to remove one and only one character to make it palindrome.
We'll use the same method we use to check if a string is palindrome or not (i.e., check the first with the last character, check 2nd with 2nd last character, and so on). Except here, we note where we get an inequal condition. And after that we check if removing either of the two makes palindrome or not.
-
To understand, consider the following example.
Clearly we need to remove the second 'a' to make it palindrome. But how will you make a program know that?
-
Lets now follow the method we described. First we check the first character with the last. Since both are a's, we move on. Second character 'a' and 2nd last 'b' dont match, so the string is not palindrome. Since removing only one character makes the string palindrome(as given in question), the character to be removed is one of these two characters. This is guaranteed to work for all strings, due to the constraints given in the question. Now you can remove the characters individually to check if string becomes palindrome, or simply observe that the characters after 'a' match with the corresponding characters starting back from 'b' on the other side, i.e.,
is palindrome. Both ways yield the same result.
-
If you don't get an inequality condition, the string is palindrome then. This problem is hence solved in linear time.