Problem · String

Set Total Palindrome Transformation Cost

Learn this problem
HardIntuit logoIntuitNEW GRADOA

Problem statement

A string is called "palindrome-like" if it can be rearranged in any way to become a palindrome. For example, aabb is palindrome-like since it can be rearranged to abba.

For any string, you are allowed to change any number of characters to make it palindrome-like. Let this minimum number of changes be the string's cost.

Given a string s, find the total sum of palindrome transformation costs of all substrings of s.

Also asked by Google: DNA Sequencing.

Function

setTotalPalindromeTransformationCost(s: String) → long

Examples

Example 1

s = "abca"return = 6

The substrings of "abca" with non-zero cost are "ab", "abc", "abca", "bc", "bca", and "ca". Each costs 1, while single-character substrings cost 0.

The total cost is therefore 6.

Constraints

  • 1 <= n <= 10^5
  • s consists of lowercase English letters

More Intuit problems

drafts saved locally
public long setTotalPalindromeTransformationCost(String s) {
  // write your code here
}
s"abca"
expected6
checking account