Shortest Packing After Removals
Packing a string writes each run of the same letter as the letter followed by the length of the run, and leaves the length out when it is 1. For example aaabcc packs to a3bc2, which is 5 characters long.
Remove at most k letters from text so that what is left packs as short as possible. Return the length of that shortest packing.
Example 1
text "aabaa"
k 1
returns 2
Remove the b. What is left, aaaa, packs to a4.
Example 2
text "aaaaaaaaaaa"
k 2
returns 2
Remove two letters. Nine a's pack to a9, one character shorter than a11.
Example 3
text "abc"
k 0
returns 3
Nothing can be removed, and abc packs to abc.
Constraints
1 <= len(text) <= 400 <= k <= len(text)textholds lowercase letters
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Strings problems
- Smallest After Moving LettersHard
- Smallest Order Without TwinsHard
- Acronym of WordsBeginner
- Alternating CaseBeginner
- Capitalize the TitleBeginner
- Center in a FieldBeginner
- Chunks From the LeftBeginner
- Consecutive CharactersBeginner