Skip to content

Instantly share code, notes, and snippets.

@primaryobjects
Created March 3, 2017 22:02
Show Gist options
  • Save primaryobjects/314a9de3600abacce7bcc7c9c83437a0 to your computer and use it in GitHub Desktop.
Save primaryobjects/314a9de3600abacce7bcc7c9c83437a0 to your computer and use it in GitHub Desktop.
Determine the index of the character whose removal will create a palindrome.
/* Method: Traverse through half the string and compare the left-side letter with the right-side letter (hence, traversing only half the string).
If the left letter does not equal the right letter, we have found the location of the removal letter to form a palindrome.
To determine which side to remove, check if the next left-side letter (i+1) matches the right-side AND we result in a complete palindrome.
Otherwise, check if the next right-side letter (str.length - 2 - j) matches the left-side AND we result in a complete palindrome.
*/
function isPalindrome(str) {
var result = true;
for (var i=0; i<str.length / 2; i++) {
if (str[i] !== str[str.length - 1 - i]) {
result = false;
break;
}
}
return result;
}
function processData(input) {
var lines = input.split('\n');
for (var i=1; i<lines.length; i++) {
var str = lines[i];
var result = -1;
for (var j=0; j<str.length / 2; j++) {
if (str[j] !== str[str.length - 1 - j]) {
if (str[j+1] === str[str.length - 1 - j] && isPalindrome(str.slice(0, j) + str.slice(j+1))) {
// Delete j (left-side).
result = j;
}
else if (str[j] === str[str.length - 2 - j] && isPalindrome(str.slice(0, str.length - 1 - j) + str.slice(str.length - j))) {
// Delete str.length - 1 - j (right-side).
result = str.length - 1 - j;
}
else {
// Not a possible palindrome.
result = -1;
}
break;
}
}
console.log(result);
}
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});
Given a string, , of lowercase letters, determine the index of the character whose removal will make a palindrome. If is already a palindrome or no such character exists, then print . There will always be a valid solution, and any correct answer is acceptable. For example, if "bcbc", we can either remove 'b' at index or 'c' at index .
Input Format
The first line contains an integer, , denoting the number of test cases.
Each line of the subsequent lines (where ) describes a test case in the form of a single string, .
Constraints
All characters are lowercase English letters.
Output Format
Print an integer denoting the zero-indexed position of the character that makes not a palindrome; if is already a palindrome or no such character exists, print .
Sample Input
3
aaab
baa
aaa
Sample Output
3
0
-1
Explanation
Test Case 1: "aaab"
Removing 'b' at index results in a palindrome, so we print on a new line.
Test Case 2: "baa"
Removing 'b' at index results in a palindrome, so we print on a new line.
Test Case 3: "aaa"
This string is already a palindrome, so we print ; however, , , and are also all acceptable answers, as the string will still be a palindrome if any one of the characters at those indices are removed.
Note: The custom checker logic for this challenge is available [here](https://gist.github.com/shashank21j/58df3865a95bf960092c).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment