FastPrepSet Total Palindrome Transformation Cost

Set Total Palindrome Transformation Cost

Intuit logoIntuit● HardNEW GRADFULLTIMEOA

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

Source note: The original source screenshot contains inconsistent example labels and character typos. The written problem and examples define the judged task.

More Intuit problems

See Intuit hiring insights
Saved
public long setTotalPalindromeTransformationCost(String s) {
  // write your code here
}
s"abca"
expected6
Checking account…