Skip to content

Commit e89c5cc

Browse files
committed
Sync LeetCode submission Runtime - 276 ms (39.72%), Memory - 28.4 MB (8.56%)
1 parent 58d99a0 commit e89c5cc

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

3451-string-compression-iii/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<p>Given a string <code>word</code>, compress it using the following algorithm:</p>
2+
3+
<ul>
4+
<li>Begin with an empty string <code>comp</code>. While <code>word</code> is <strong>not</strong> empty, use the following operation:
5+
6+
<ul>
7+
<li>Remove a maximum length prefix of <code>word</code> made of a <em>single character</em> <code>c</code> repeating <strong>at most</strong> 9 times.</li>
8+
<li>Append the length of the prefix followed by <code>c</code> to <code>comp</code>.</li>
9+
</ul>
10+
</li>
11+
</ul>
12+
13+
<p>Return the string <code>comp</code>.</p>
14+
15+
<p>&nbsp;</p>
16+
<p><strong class="example">Example 1:</strong></p>
17+
18+
<div class="example-block">
19+
<p><strong>Input:</strong> <span class="example-io">word = &quot;abcde&quot;</span></p>
20+
21+
<p><strong>Output:</strong> <span class="example-io">&quot;1a1b1c1d1e&quot;</span></p>
22+
23+
<p><strong>Explanation:</strong></p>
24+
25+
<p>Initially, <code>comp = &quot;&quot;</code>. Apply the operation 5 times, choosing <code>&quot;a&quot;</code>, <code>&quot;b&quot;</code>, <code>&quot;c&quot;</code>, <code>&quot;d&quot;</code>, and <code>&quot;e&quot;</code> as the prefix in each operation.</p>
26+
27+
<p>For each prefix, append <code>&quot;1&quot;</code> followed by the character to <code>comp</code>.</p>
28+
</div>
29+
30+
<p><strong class="example">Example 2:</strong></p>
31+
32+
<div class="example-block">
33+
<p><strong>Input:</strong> <span class="example-io">word = &quot;aaaaaaaaaaaaaabb&quot;</span></p>
34+
35+
<p><strong>Output:</strong> <span class="example-io">&quot;9a5a2b&quot;</span></p>
36+
37+
<p><strong>Explanation:</strong></p>
38+
39+
<p>Initially, <code>comp = &quot;&quot;</code>. Apply the operation 3 times, choosing <code>&quot;aaaaaaaaa&quot;</code>, <code>&quot;aaaaa&quot;</code>, and <code>&quot;bb&quot;</code> as the prefix in each operation.</p>
40+
41+
<ul>
42+
<li>For prefix <code>&quot;aaaaaaaaa&quot;</code>, append <code>&quot;9&quot;</code> followed by <code>&quot;a&quot;</code> to <code>comp</code>.</li>
43+
<li>For prefix <code>&quot;aaaaa&quot;</code>, append <code>&quot;5&quot;</code> followed by <code>&quot;a&quot;</code> to <code>comp</code>.</li>
44+
<li>For prefix <code>&quot;bb&quot;</code>, append <code>&quot;2&quot;</code> followed by <code>&quot;b&quot;</code> to <code>comp</code>.</li>
45+
</ul>
46+
</div>
47+
48+
<p>&nbsp;</p>
49+
<p><strong>Constraints:</strong></p>
50+
51+
<ul>
52+
<li><code>1 &lt;= word.length &lt;= 2 * 10<sup>5</sup></code></li>
53+
<li><code>word</code> consists only of lowercase English letters.</li>
54+
</ul>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Approach: String Manipulation
2+
3+
# Time: O(n)
4+
# Space: O(1)
5+
6+
class Solution:
7+
def compressedString(self, word: str) -> str:
8+
comp = []
9+
10+
pos = 0
11+
12+
while pos < len(word):
13+
consecutive_count = 0
14+
current_char = word[pos]
15+
16+
while (pos < len(word) and consecutive_count < 9 and word[pos] == current_char):
17+
consecutive_count += 1
18+
pos += 1
19+
20+
comp.append(str(consecutive_count))
21+
comp.append(current_char)
22+
23+
return ''.join(comp)

0 commit comments

Comments
 (0)