AmazonOA #335
Minimum Operations to Remove Characters in Frequency Order (Find Min Deletions :)
Estimated HardJudge-ready
Topics (AI-labelled)String DP
Each time, choose a non-empty contiguous substring of the current string; it can be deleted only if all of its characters are the same. After a deletion the left and right parts are joined, which may form a new contiguous run of the same character. Find the minimum number of deletions needed to empty the string. The frequency order mentioned in the title adds no extra requirement on the operations; deletions are not ordered by character frequency.
Input
The input is a JSON string. On this site 0≤length≤200, characters are counted as Unicode characters, and the empty string is valid. Whitespace and non-ASCII characters in the JSON string must be escaped, with a space written as \u0020.
Output
Output a single integer, the answer.
Example 1
Input:
"abaca"
Output:
3
Example 2
Input:
"aba"
Output:
2
Example 3
Input:
""
Output:
0
Example notes
The answers are 3, 2 and 0. For abaca, delete b and c first, then delete aaa in one go; counting the original same-character runs, as the source does, wrongly gives 5, so the joining after deletions cannot be ignored.
Editorial
Loading solution…