Compute maximum beauty of a string
Company: Bank of America
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Take-home Project
Quick Answer: This question evaluates skills in string manipulation, frequency analysis, and optimization of weighted sums, focusing on assigning distinct values to characters to maximize an aggregate metric.
Constraints
- The string may contain uppercase letters, lowercase letters, spaces, punctuation, and digits.
- Letter comparison is case-insensitive: 'A' and 'a' count as the same letter.
- Only alphabetic characters (a-z, A-Z) contribute to beauty; everything else is ignored.
- There are at most 26 distinct letters, so beauty values 26, 25, ... are assigned greedily by descending frequency.
- An empty string, or a string with no letters, has a beauty of 0.
Examples
Input: "ABbCCC"
Expected Output: 152
Explanation: Frequencies C:3, B:2, A:1. Assign 26*3 + 25*2 + 24*1 = 78 + 50 + 24 = 152.
Input: "Ignore punctuation, please :)"
Expected Output: 491
Explanation: Only letters count; the comma, space, and ':)' are ignored. Greedily assigning 26, 25, 24, ... to letters by descending frequency yields 491.
Input: ""
Expected Output: 0
Explanation: An empty string has no letters, so its beauty is 0.
Input: "!!! ,,, 12345"
Expected Output: 0
Explanation: There are no alphabetic characters; punctuation, spaces, and digits are all ignored, so the beauty is 0.
Input: "a"
Expected Output: 26
Explanation: A single distinct letter gets the maximum beauty value 26, appearing once: 26*1 = 26.
Input: "aaaA"
Expected Output: 104
Explanation: Case-insensitively, 'a' appears 4 times. The single distinct letter gets value 26: 26*4 = 104.
Hints
- The beauty assignment is a permutation of 1..26 over the distinct letters. To maximize a sum of (value x frequency), pair the largest values with the largest frequencies (rearrangement inequality).
- You never need to know which specific letter gets which value -- only the multiset of letter frequencies matters.
- Count case-insensitive letter frequencies, sort them in descending order, then multiply the i-th largest frequency by (26 - i) and sum.
- Be careful to ignore spaces, punctuation, and digits, and to treat uppercase and lowercase as the same letter.