Shortest Compressed Length After Removal
Updated
Problem statement
A compressed representation replaces each maximal run of equal consecutive characters as follows:
- A run of length
1is represented by the character alone. - A run longer than
1is represented by its decimal length followed by the character.
For example, ABBBCCCC compresses to A3B4C.
Given a string S of length N and an integer K, remove exactly K consecutive characters from S. Return the shortest possible length of the compressed representation of the remaining string.
The brackets in each HTML diagram mark the consecutive characters removed in that example.
┌───────────────┐ ┌───────┐
│ ABBBCC[DDC]CC │ ──▶ │ A3B4C │
└───────────────┘ └───────┘┌──────────────────────────┐ ┌─────┐
│ AAAAAAAAAAA[BXX]AAAAAAAAAA │ ──▶ │ 21A │
└──────────────────────────┘ └─────┘┌────────────┐ ┌───────┐
│ ABCDDD[EF]G │ ──▶ │ ABC3DG │
└────────────┘ └───────┘Function
solution(S: String, K: int) → intExamples
Example 1
S = "ABBBCCDDCCC"K = 3return = 5Remove DDC to obtain ABBBCCCC. It compresses to A3B4C, whose length is 5.
Example 2
S = "AAAAAAAAAAABXXAAAAAAAAAA"K = 3return = 3Remove BXX to leave twenty-one consecutive A characters. They compress to 21A, whose length is 3.
Example 3
S = "ABCDDDEFG"K = 2return = 6Remove EF to obtain ABCDDDG. It compresses to ABC3DG, whose length is 6.
More DRW Online Assessment questions
- Count Balanced Nodes in a Rooted TreeOA · Seen Sep 2026
- Largest Number With Even Digit FrequenciesOA · Seen Sep 2026
- Shortest Path Around Rectangular ObstaclesOA · Seen Sep 2026
- 2020 Account Balance with Monthly Card FeesOA · Seen Aug 2026
- Nested In-Memory Database TransactionsOA · Seen Aug 2026
- Password Requirement CheckOA · Seen Aug 2026
- Remove Excess Consecutive CharactersOA · Seen Aug 2026
- Last Round for Each PlayerOA · Seen Jul 2026
- Doctor Appointment Slot AssignmentOA · Seen Jul 2026
- Maximum Even-Sum Neighboring PairsOA · Seen Jul 2026
Similar questions at other companies
- Earliest Time to Schedule a MeetingMathWorks · OAMathWorks interview questions
- Earliest Meeting Start on a 24-Hour ClockIMC Trading · OAIMC Trading interview questions
- Earliest Time to Schedule a MeetingMicrosoft · OAMicrosoft interview questions
- Maximum Equalized Substrings After One Adjacent SwapVisa · OAVisa interview questions
- Maximum Redundancy After One Segment FlipBloomberg LP · OABloomberg LP interview questions
- Balanced Split String with WildcardsTwo Sigma · OATwo Sigma interview questions
- Longest Balanced Substring After One SwapTekion · OATekion interview questions
- Count Special StringsAmazon · OA
- Binary Palindromic Subsequences of Length 5Citadel · OACitadel interview questions
- Maximum Positive FeedbackTikTok · OATikTok interview questions