In Santa's workshop, the elves are known for their love of puzzles and brain teasers. This year, they've come up with a special challenge: to create a Christmas palindrome.
A palindrome is a word that reads the same forwards and backward. The task is to determine if it's possible to transform a given string into a palindrome with, at most, a single exchange of letters.
/**
* Determines if it's possible to form a palindrome with at most one letter exchange.
* @param {string} word - The input string.
* @returns {(number[]|null)} - Returns an array of two indices if a palindrome can be formed with one change,
* an empty array if the input is already a palindrome, or null if it's not possible.
*/
function getIndexsForPalindrome(word) {
// Implementation goes here
}
getIndexsForPalindrome('anna'); // Returns: []
getIndexsForPalindrome('abab'); // Returns: [0, 1]
getIndexsForPalindrome('abac'); // Returns: null
getIndexsForPalindrome('aaaaaaaa'); // Returns: []
getIndexsForPalindrome('aaababa'); // Returns: [1, 3]
getIndexsForPalindrome('caababa'); // Returns: null
- If the input string is already a palindrome, the function returns an empty array.
- If it's not possible to form a palindrome, the function returns null.
- If a palindrome can be formed with one change, the function returns an array with the two positions (indexes) that must be swapped to create it.
Note:
- The function should always return the first valid pair of indices if multiple swaps are possible.
- Ensure the provided examples match the expected output.
- Feel free to add more examples or edge cases to thoroughly test the function.