Class Solution
-
- All Implemented Interfaces:
public final class Solution
3163 - String Compression III\.
Medium
Given a string
word
, compress it using the following algorithm:Begin with an empty string
comp
. Whileword
is not empty, use the following operation:
Return the string
comp
.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 tocomp
.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.For prefix
"aaaaaaaaa"
, append"9"
followed by"a"
tocomp
.For prefix
"aaaaa"
, append"5"
followed by"a"
tocomp
.For prefix
"bb"
, append"2"
followed by"b"
tocomp
.
Constraints:
<code>1 <= word.length <= 2 * 10<sup>5</sup></code>
word
consists only of lowercase English letters.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final String
compressedString(String word)
-
-
Method Detail
-
compressedString
final String compressedString(String word)
-
-
-
-