FastPrepBreak a Palindrome

Break a Palindrome

AT&T logoAT&T● MediumNEW GRADOA
Learn

Problem statement

You are given a palindromic string palindrome containing only lowercase English letters.

Replace exactly one character with another lowercase English letter so that the resulting string is not a palindrome. Return the lexicographically smallest result that can be obtained. If no valid result exists, return the empty string.

Function

breakPalindrome(palindrome: String) → String

Examples

Example 1

palindrome = "abccba"return = "aaccba"

Changing the first non-a character in the first half to a produces the smallest non-palindrome.

Example 2

palindrome = "a"return = ""

Every one-character string is a palindrome, so no valid replacement exists.

Example 3

palindrome = "aa"return = "ab"

No character in the first half can be lowered, so changing the last character to b yields the smallest valid result.

Constraints

  • 1 <= palindrome.length <= 1000.
  • palindrome contains only lowercase English letters.
  • palindrome is a palindrome.

More AT&T problems

See AT&T hiring insights
public String breakPalindrome(String palindrome) {
  // write your code here
}
palindrome"abccba"
expected"aaccba"
Checking account…