Shortest Compressed String After Removal
Learn this problemProblem statement
Strings with long blocks of repeating characters take much less space if kept in a compressed representation. To obtain the compressed representation, we replace each segment of equal characters in the string with the number of characters in the segment followed by the character (for example, we replace segment "CCCC" with "4C"). To avoid increasing the size, we leave the one-letter segments unchanged (the compressed representation of "BC" is the same string - "BC").
For example, the compressed representation of the string "ABBBCCDDDCCC" is "A3B2C2D3C", and the compressed representation of the string "AAAAAAAAAABXXAAAAAAAAAA" is "11AB2X10A".
Observe that, in the second example, if we removed the "BXX" segment from the middle of the word, we would obtain a much shorter compressed representation - "21A". In order to take advantage of this observation, we decided to modify our compression algorithm.
Now, before compression, we remove exactly K consecutive letters from the input string. We would like to know the shortest compressed form that we can generate this way.
Given a string S of length N and an integer K, returns the shortest possible length of the compressed representation of S after removing exactly K consecutive characters from S.
Function
shortestPossibleLength(S: String, K: int) → intExamples
Example 1
S = "ABBBCCDDCCC"K = 3return = 5Example 2
S = "AAAAAAAAAABXXAAAAAAAAAA"K = 3return = 3Example 3
S = "ABCDDDEFG"K = 2return = 6Constraints
More Microsoft problems
- Maximum Pipeline ThroughputOA · Seen Jul 2026
- Maximum Strong Team SubarrayOA · Seen Jul 2026
- Minimum Cost K-Capable ModelsOA · Seen Jul 2026
- Alphabetically Smallest PalindromeOA · Seen Jul 2026
- Maximum Reward PointsOA · Seen Jul 2026
- Maximum Strength of Every NeuronOA · Seen Jul 2026
- Neural Network Subnetwork StrengthOA · Seen Jul 2026
- XOR MultiplicationOA · Seen Jul 2026