String Compression III

Given a string `word`, build a string `comp` as follows. Start with `comp` empty. While `word` is not empty, remove the longest prefix of `word` made of a single character `c` repeated at most 9 times, and append the length of that prefix followed by `c` to `comp`. Return `comp`.

30:00

String Compression III
medium
Free written solution
Topics
Companies

Given a string word, build a string comp as follows. Start with comp empty. While word is not empty, remove the longest prefix of word made of a single character c repeated at most 9 times, and append the length of that prefix followed by c to comp. Return comp.

Example 1:
Input: {"word":"abcde"}
Output: 1a1b1c1d1e
Constraints:
  • 1≤word.length≤2⋅1051 \leq word.\text{length} \leq 2 \cdot 10^5

  • word consists only of lowercase English letters.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"word":"abcde"}

word (5 letters): packed · window i..j

a

1a

b

1b

c

1c

d

1d

e

1e

comp starts empty. Each piece will be a count from 1 to 9 followed by one letter.

comp (0 pieces, 0 characters)

Variables
VariableValue
i0
j0
comp""
DepthFunction Call
Stack empty
0/16