Skip to content

Commit e323d06

Browse files
authored
feat: add solutions to lc problem: No.3163 (doocs#2929)
No.3163.String Compression III
1 parent 78497a2 commit e323d06

File tree

7 files changed

+292
-8
lines changed

7 files changed

+292
-8
lines changed

solution/3100-3199/3163.String Compression III/README.md

+101-4
Original file line numberDiff line numberDiff line change
@@ -80,32 +80,129 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3163.St
8080

8181
<!-- solution:start -->
8282

83-
### 方法一
83+
### 方法一:双指针
84+
85+
我们可以利用双指针,统计每个字符的连续出现次数。假如当前字符 $c$ 连续出现了 $k$ 次,然后我们将 $k$ 划分成若干个 $x$,每个 $x$ 最大为 $9$,然后将 $x$ 和 $c$ 拼接起来,将每个 $x$ 和 $c$ 拼接起来到结果中。
86+
87+
最后返回结果即可。
88+
89+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 `word` 的长度。
8490

8591
<!-- tabs:start -->
8692

8793
#### Python3
8894

8995
```python
90-
96+
class Solution:
97+
def compressedString(self, word: str) -> str:
98+
g = groupby(word)
99+
ans = []
100+
for c, v in g:
101+
k = len(list(v))
102+
while k:
103+
x = min(9, k)
104+
ans.append(str(x) + c)
105+
k -= x
106+
return "".join(ans)
91107
```
92108

93109
#### Java
94110

95111
```java
96-
112+
class Solution {
113+
public String compressedString(String word) {
114+
StringBuilder ans = new StringBuilder();
115+
int n = word.length();
116+
for (int i = 0; i < n;) {
117+
int j = i + 1;
118+
while (j < n && word.charAt(j) == word.charAt(i)) {
119+
++j;
120+
}
121+
int k = j - i;
122+
while (k > 0) {
123+
int x = Math.min(9, k);
124+
ans.append(x).append(word.charAt(i));
125+
k -= x;
126+
}
127+
i = j;
128+
}
129+
return ans.toString();
130+
}
131+
}
97132
```
98133

99134
#### C++
100135

101136
```cpp
102-
137+
class Solution {
138+
public:
139+
string compressedString(string word) {
140+
string ans;
141+
int n = word.length();
142+
for (int i = 0; i < n;) {
143+
int j = i + 1;
144+
while (j < n && word[j] == word[i]) {
145+
++j;
146+
}
147+
int k = j - i;
148+
while (k > 0) {
149+
int x = min(9, k);
150+
ans.push_back('0' + x);
151+
ans.push_back(word[i]);
152+
k -= x;
153+
}
154+
i = j;
155+
}
156+
return ans;
157+
}
158+
};
103159
```
104160
105161
#### Go
106162
107163
```go
164+
func compressedString(word string) string {
165+
ans := []byte{}
166+
n := len(word)
167+
for i := 0; i < n; {
168+
j := i + 1
169+
for j < n && word[j] == word[i] {
170+
j++
171+
}
172+
k := j - i
173+
for k > 0 {
174+
x := min(9, k)
175+
ans = append(ans, byte('0'+x))
176+
ans = append(ans, word[i])
177+
k -= x
178+
}
179+
i = j
180+
}
181+
return string(ans)
182+
}
183+
```
108184

185+
#### TypeScript
186+
187+
```ts
188+
function compressedString(word: string): string {
189+
const ans: string[] = [];
190+
const n = word.length;
191+
for (let i = 0; i < n; ) {
192+
let j = i + 1;
193+
while (j < n && word[j] === word[i]) {
194+
++j;
195+
}
196+
let k = j - i;
197+
while (k) {
198+
const x = Math.min(k, 9);
199+
ans.push(x + word[i]);
200+
k -= x;
201+
}
202+
i = j;
203+
}
204+
return ans.join('');
205+
}
109206
```
110207

111208
<!-- tabs:end -->

solution/3100-3199/3163.String Compression III/README_EN.md

+101-4
Original file line numberDiff line numberDiff line change
@@ -76,32 +76,129 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3163.St
7676

7777
<!-- solution:start -->
7878

79-
### Solution 1
79+
### Solution 1: Two Pointers
80+
81+
We can use two pointers to count the consecutive occurrences of each character. Suppose the current character $c$ appears consecutively $k$ times, then we divide $k$ into several $x$, each $x$ is at most $9$, then we concatenate $x$ and $c$, and append each $x$ and $c$ to the result.
82+
83+
Finally, return the result.
84+
85+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the
8086

8187
<!-- tabs:start -->
8288

8389
#### Python3
8490

8591
```python
86-
92+
class Solution:
93+
def compressedString(self, word: str) -> str:
94+
g = groupby(word)
95+
ans = []
96+
for c, v in g:
97+
k = len(list(v))
98+
while k:
99+
x = min(9, k)
100+
ans.append(str(x) + c)
101+
k -= x
102+
return "".join(ans)
87103
```
88104

89105
#### Java
90106

91107
```java
92-
108+
class Solution {
109+
public String compressedString(String word) {
110+
StringBuilder ans = new StringBuilder();
111+
int n = word.length();
112+
for (int i = 0; i < n;) {
113+
int j = i + 1;
114+
while (j < n && word.charAt(j) == word.charAt(i)) {
115+
++j;
116+
}
117+
int k = j - i;
118+
while (k > 0) {
119+
int x = Math.min(9, k);
120+
ans.append(x).append(word.charAt(i));
121+
k -= x;
122+
}
123+
i = j;
124+
}
125+
return ans.toString();
126+
}
127+
}
93128
```
94129

95130
#### C++
96131

97132
```cpp
98-
133+
class Solution {
134+
public:
135+
string compressedString(string word) {
136+
string ans;
137+
int n = word.length();
138+
for (int i = 0; i < n;) {
139+
int j = i + 1;
140+
while (j < n && word[j] == word[i]) {
141+
++j;
142+
}
143+
int k = j - i;
144+
while (k > 0) {
145+
int x = min(9, k);
146+
ans.push_back('0' + x);
147+
ans.push_back(word[i]);
148+
k -= x;
149+
}
150+
i = j;
151+
}
152+
return ans;
153+
}
154+
};
99155
```
100156
101157
#### Go
102158
103159
```go
160+
func compressedString(word string) string {
161+
ans := []byte{}
162+
n := len(word)
163+
for i := 0; i < n; {
164+
j := i + 1
165+
for j < n && word[j] == word[i] {
166+
j++
167+
}
168+
k := j - i
169+
for k > 0 {
170+
x := min(9, k)
171+
ans = append(ans, byte('0'+x))
172+
ans = append(ans, word[i])
173+
k -= x
174+
}
175+
i = j
176+
}
177+
return string(ans)
178+
}
179+
```
104180

181+
#### TypeScript
182+
183+
```ts
184+
function compressedString(word: string): string {
185+
const ans: string[] = [];
186+
const n = word.length;
187+
for (let i = 0; i < n; ) {
188+
let j = i + 1;
189+
while (j < n && word[j] === word[i]) {
190+
++j;
191+
}
192+
let k = j - i;
193+
while (k) {
194+
const x = Math.min(k, 9);
195+
ans.push(x + word[i]);
196+
k -= x;
197+
}
198+
i = j;
199+
}
200+
return ans.join('');
201+
}
105202
```
106203

107204
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
string compressedString(string word) {
4+
string ans;
5+
int n = word.length();
6+
for (int i = 0; i < n;) {
7+
int j = i + 1;
8+
while (j < n && word[j] == word[i]) {
9+
++j;
10+
}
11+
int k = j - i;
12+
while (k > 0) {
13+
int x = min(9, k);
14+
ans.push_back('0' + x);
15+
ans.push_back(word[i]);
16+
k -= x;
17+
}
18+
i = j;
19+
}
20+
return ans;
21+
}
22+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
func compressedString(word string) string {
2+
ans := []byte{}
3+
n := len(word)
4+
for i := 0; i < n; {
5+
j := i + 1
6+
for j < n && word[j] == word[i] {
7+
j++
8+
}
9+
k := j - i
10+
for k > 0 {
11+
x := min(9, k)
12+
ans = append(ans, byte('0'+x))
13+
ans = append(ans, word[i])
14+
k -= x
15+
}
16+
i = j
17+
}
18+
return string(ans)
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public String compressedString(String word) {
3+
StringBuilder ans = new StringBuilder();
4+
int n = word.length();
5+
for (int i = 0; i < n;) {
6+
int j = i + 1;
7+
while (j < n && word.charAt(j) == word.charAt(i)) {
8+
++j;
9+
}
10+
int k = j - i;
11+
while (k > 0) {
12+
int x = Math.min(9, k);
13+
ans.append(x).append(word.charAt(i));
14+
k -= x;
15+
}
16+
i = j;
17+
}
18+
return ans.toString();
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def compressedString(self, word: str) -> str:
3+
g = groupby(word)
4+
ans = []
5+
for c, v in g:
6+
k = len(list(v))
7+
while k:
8+
x = min(9, k)
9+
ans.append(str(x) + c)
10+
k -= x
11+
return "".join(ans)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function compressedString(word: string): string {
2+
const ans: string[] = [];
3+
const n = word.length;
4+
for (let i = 0; i < n; ) {
5+
let j = i + 1;
6+
while (j < n && word[j] === word[i]) {
7+
++j;
8+
}
9+
let k = j - i;
10+
while (k) {
11+
const x = Math.min(k, 9);
12+
ans.push(x + word[i]);
13+
k -= x;
14+
}
15+
i = j;
16+
}
17+
return ans.join('');
18+
}

0 commit comments

Comments
 (0)