Problem · String

Break a Palindrome

Learn this problem
HardRoblox logoRobloxINTERNOA
See Roblox hiring insights

Problem statement

You are given a lowercase string palindrome that is a palindrome. Change exactly one character so that the result is not a palindrome.

Among all valid results, return the lexicographically smallest one. If changing exactly one character cannot produce a non-palindrome, return "IMPOSSIBLE".

Two strings of the same length are compared lexicographically by the first position at which they differ.

Function

breakPalindrome(palindrome: String) → String

Examples

Example 1

palindrome = "abccba"return = "aaccba"

Changing the first non-'a' character in the left half gives the smallest possible non-palindrome.

Example 2

palindrome = "a"return = "IMPOSSIBLE"

Every one-character string is a palindrome, so no one-character change can make it non-palindromic.

Example 3

palindrome = "aa"return = "ab"

The left character is already 'a'; changing the final character to 'b' gives the smallest valid result.

Example 4

palindrome = "aba"return = "abb"

Changing the middle character would preserve the palindrome, so the smallest valid choice is to change the final character to 'b'.

Constraints

  • 1 <= palindrome.length <= 100000
  • palindrome contains lowercase English letters.
  • palindrome is a palindrome.
  • You must change exactly one character.

More Roblox problems

drafts saved locally
public String breakPalindrome(String palindrome) {
  // write your code here
}
palindrome"abccba"
expected"aaccba"
checking account