Skip to content

Commit 2c666e0

Browse files
committed
solution added for 3163
1 parent b6acb73 commit 2c666e0

File tree

1 file changed

+241
-0
lines changed

1 file changed

+241
-0
lines changed
Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
---
2+
id: string-compression
3+
title: String Compression Solution
4+
sidebar_label: String Compression
5+
tags:
6+
- String Compression
7+
- Brute Force
8+
- Optimized
9+
- LeetCode
10+
- Java
11+
- Python
12+
- C++
13+
description: "This is a solution to the String Compression problem on LeetCode."
14+
sidebar_position: 2
15+
---
16+
17+
In this tutorial, we will solve the String Compression problem using two different approaches: brute force and optimized. We will provide the implementation of the solution in C++, Java, and Python.
18+
19+
## Problem Description
20+
21+
Given a string `word`, compress it using the following algorithm:
22+
23+
Begin with an empty string `comp`. While `word` is not empty, use the following operation:
24+
Remove a maximum length prefix of `word` made of a single character `c` repeating at most 9 times.
25+
Append the length of the prefix followed by `c` to `comp`.
26+
27+
Return the string `comp`.
28+
29+
### Examples
30+
31+
**Example 1:**
32+
33+
```
34+
Input: word = "abcde"
35+
Output: "1a1b1c1d1e"
36+
```
37+
38+
**Example 2:**
39+
40+
```
41+
Input: word = "aaaaaaaaaaaaaabb"
42+
Output: "9a5a2b"
43+
```
44+
45+
### Constraints
46+
47+
- `1 <= word.length <= 2 * 105`
48+
- `word` consists only of lowercase English letters.
49+
50+
---
51+
52+
## Solution for String Compression Problem
53+
54+
### Intuition and Approach
55+
56+
The problem can be solved using a brute force approach or an optimized approach. The brute force approach directly iterates through the string and constructs the result, while the optimized approach efficiently handles consecutive characters.
57+
58+
<Tabs>
59+
<tabItem value="Brute Force" label="Brute Force">
60+
61+
### Approach 1: Brute Force (Naive)
62+
63+
The brute force approach iterates through each character of the string, counts consecutive characters up to 9, and appends the count followed by the character to the result string.
64+
65+
#### Code in Different Languages
66+
67+
<Tabs>
68+
<TabItem value="C++" label="C++" default>
69+
<SolutionAuthor name="@ImmidiSivani"/>
70+
71+
```cpp
72+
class Solution {
73+
public:
74+
string compressString(string word) {
75+
string comp;
76+
int i = 0;
77+
while (i < word.length()) {
78+
char c = word[i];
79+
int count = 0;
80+
while (i < word.length() && word[i] == c && count < 9) {
81+
count++;
82+
i++;
83+
}
84+
comp += to_string(count) + c;
85+
}
86+
return comp;
87+
}
88+
};
89+
```
90+
91+
</TabItem>
92+
<TabItem value="Java" label="Java">
93+
<SolutionAuthor name="@ImmidiSivani"/>
94+
95+
```java
96+
class Solution {
97+
public String compressString(String word) {
98+
StringBuilder comp = new StringBuilder();
99+
int i = 0;
100+
while (i < word.length()) {
101+
char c = word.charAt(i);
102+
int count = 0;
103+
while (i < word.length() && word.charAt(i) == c && count < 9) {
104+
count++;
105+
i++;
106+
}
107+
comp.append(count).append(c);
108+
}
109+
return comp.toString();
110+
}
111+
}
112+
```
113+
114+
</TabItem>
115+
<TabItem value="Python" label="Python">
116+
<SolutionAuthor name="@ImmidiSivani"/>
117+
118+
```python
119+
class Solution:
120+
def compressString(self, word: str) -> str:
121+
comp = []
122+
i = 0
123+
while i < len(word):
124+
c = word[i]
125+
count = 0
126+
while i < len(word) and word[i] == c and count < 9:
127+
count += 1
128+
i += 1
129+
comp.append(f"{count}{c}")
130+
return ''.join(comp)
131+
```
132+
133+
</TabItem>
134+
</Tabs>
135+
136+
#### Complexity Analysis
137+
138+
- Time Complexity: $O(n)$
139+
- Space Complexity: $O(n)$
140+
- Where `n` is the length of `word`.
141+
- The time complexity is $O(n)$ because we iterate through the string once.
142+
- The space complexity is $O(n)$ because we store the result in a new string.
143+
144+
</tabItem>
145+
<tabItem value="Optimized" label="Optimized">
146+
147+
### Approach 2: Optimized Approach
148+
149+
The optimized approach is similar to the brute force but handles the prefix length more efficiently, ensuring it counts up to 9 characters in each iteration.
150+
151+
#### Code in Different Languages
152+
153+
<Tabs>
154+
<TabItem value="C++" label="C++" default>
155+
<SolutionAuthor name="@ImmidiSivani"/>
156+
157+
```cpp
158+
class Solution {
159+
public:
160+
string compressString(string word) {
161+
string comp;
162+
int i = 0;
163+
while (i < word.length()) {
164+
char c = word[i];
165+
int count = 1;
166+
while (i + count < word.length() && word[i + count] == c && count < 9) {
167+
count++;
168+
}
169+
comp += to_string(count) + c;
170+
i += count;
171+
}
172+
return comp;
173+
}
174+
};
175+
```
176+
177+
</TabItem>
178+
<TabItem value="Java" label="Java">
179+
<SolutionAuthor name="@ImmidiSivani"/>
180+
181+
```java
182+
class Solution {
183+
public String compressString(String word) {
184+
StringBuilder comp = new StringBuilder();
185+
int i = 0;
186+
while (i < word.length()) {
187+
char c = word.charAt(i);
188+
int count = 1;
189+
while (i + count < word.length() && word.charAt(i + count) == c && count < 9) {
190+
count++;
191+
}
192+
comp.append(count).append(c);
193+
i += count;
194+
}
195+
return comp.toString();
196+
}
197+
}
198+
```
199+
200+
</TabItem>
201+
<TabItem value="Python" label="Python">
202+
<SolutionAuthor name="@ImmidiSivani"/>
203+
204+
```python
205+
class Solution:
206+
def compressString(self, word: str) -> str:
207+
comp = []
208+
i = 0
209+
while i < len(word):
210+
c = word[i]
211+
count = 1
212+
while i + count < len(word) and word[i + count] == c and count < 9:
213+
count += 1
214+
comp.append(f"{count}{c}")
215+
i += count
216+
return ''.join(comp)
217+
```
218+
219+
</TabItem>
220+
</Tabs>
221+
222+
#### Complexity Analysis
223+
224+
- Time Complexity: $O(n)$
225+
- Space Complexity: $O(n)$
226+
- Where `n` is the length of `word`.
227+
- The time complexity is $O(n)$ because we iterate through the string once.
228+
- The space complexity is $O(n)$ because we store the result in a new string.
229+
230+
</tabItem>
231+
</Tabs>
232+
233+
---
234+
235+
<h2>Authors:</h2>
236+
237+
<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}>
238+
{['ImmidiSivani'].map(username => (
239+
<Author key={username} username={username} />
240+
))}
241+
</div>

0 commit comments

Comments
 (0)