Trains the technique from
LeetCode 3163String Compression IIIThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A tape of lowercase letters reads tape. Write its shorthand by repeating the following until nothing is left of the tape: take off the longest stretch at the front made of one repeated letter, but never more than nine of it, and append to the shorthand how many were taken followed by that letter.
Return the shorthand.
Example 1
Three stretches of two letters each, so each is written as its count followed by its letter.
Example 2
Ten of the same letter, but a stretch may hold at most nine, so nine come off first and the last one follows as a stretch of its own.
Example 3
A single letter is a stretch of one.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def compressed_string(tape: str) -> str:public String compressedString(String tape)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.