mediumString 0 views

String Compression III

Given a string word, compress it using the following algorithm: Return the string comp.

Given a string word, compress it using the following algorithm:

Return the string comp.

String Compression III diagram

Example 1

Input: word = "abcde"

Output: "1a1b1c1d1e"

Explanation: Initially, comp = "" . Apply the operation 5 times, choosing "a" , "b" , "c" , "d" , and "e" as the prefix in each operation. For each prefix, append "1" followed by the character to comp .

Example 2

Input: word = "aaaaaaaaaaaaaabb"

Output: "9a5a2b"

Explanation: Initially, comp = "" . Apply the operation 3 times, choosing "aaaaaaaaa" , "aaaaa" , and "bb" as the prefix in each operation.

Constraints

  • 1 <= word.length <= 2 * 10^5
  • word consists only of lowercase English letters.

Hints

Companies

No companies reported yet.

Discussion

Sign in to join the discussion.

Loading discussion...

Test results

No test cases yet.