FastPrepCount Palindrome Addition Choices

Count Palindrome Addition Choices

Hudson River Trading logoHudson River Trading● EasyFULLTIMEOA

Problem statement

You are given a string inputStr. Choose either to add no letter or to add one lowercase English letter. Count the choices for which the resulting letters can be rearranged into a palindrome.

Adding the same letter at different positions counts as one choice. Different palindrome arrangements also count as the same choice. The answer is at most 27: one choice to add nothing and 26 choices of a letter.

Return the number of valid choices.

Function

countPalindromeAdditions(inputStr: String) → int

Examples

Example 1

inputStr = "aabb"return = 27

Both letter counts are even, so adding nothing works. Adding any one of the 26 letters leaves exactly one odd count, which also allows a palindrome. There are 1 + 26 = 27 choices.

Example 2

inputStr = "abbb"return = 2

The counts of a and b are both odd. Adding either a or b leaves just one odd count. These choices can form abbba and bbabb, respectively. Adding nothing or another letter does not work.

Constraints

  • 1 <= inputStr.length <= 100
  • inputStr contains only lowercase English letters.

More Hudson River Trading problems

See Hudson River Trading hiring insights
Saved
public int countPalindromeAdditions(String inputStr) {
    // Return the number of valid choices.
}
Checking account…