Skip to content

Commit 9e16108

Browse files
committed
feat: add python and java solutions to lcci question
添加《程序员面试金典》题解:面试题 01.06. 字符串压缩
1 parent 043c3d5 commit 9e16108

File tree

4 files changed

+162
-53
lines changed

4 files changed

+162
-53
lines changed

lcci/01.06.Compress String/README.md

+34-3
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,51 @@
2828

2929
## 解法
3030
<!-- 这里可写通用的实现逻辑 -->
31-
31+
双指针遍历字符串求解。
3232

3333
### Python3
3434
<!-- 这里可写当前语言的特殊实现逻辑 -->
3535

3636
```python
37-
37+
class Solution:
38+
def compressString(self, S: str) -> str:
39+
if len(S) < 2:
40+
return S
41+
p, q = 0, 1
42+
res = ''
43+
while q < len(S):
44+
if S[p] != S[q]:
45+
res += (S[p] + str(q - p))
46+
p = q
47+
q += 1
48+
res += (S[p] + str(q - p))
49+
return res if len(res) < len(S) else S
3850
```
3951

4052
### Java
4153
<!-- 这里可写当前语言的特殊实现逻辑 -->
4254

4355
```java
44-
56+
class Solution {
57+
public String compressString(String S) {
58+
if (S == null || S.length() < 2) {
59+
return S;
60+
}
61+
char[] chars = S.toCharArray();
62+
int p = 0, q = 1, n = chars.length;
63+
StringBuilder sb = new StringBuilder();
64+
while (q < n) {
65+
if (chars[p] != chars[q]) {
66+
sb.append(chars[p]).append(q - p);
67+
p = q;
68+
}
69+
q += 1;
70+
}
71+
sb.append(chars[p]).append(q - p);
72+
String res = sb.toString();
73+
return res.length() < n ? res : S;
74+
}
75+
}
4576
```
4677

4778
### ...
+95-50
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,95 @@
1-
# [01.06. Compress String](https://leetcode-cn.com/problems/compress-string-lcci)
2-
3-
## Description
4-
<p>Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would become a2blc5a3. If the &quot;compressed&quot; string would not become smaller than the original string, your method should return the original string. You can assume the string has only uppercase and lowercase letters (a - z).</p>
5-
6-
<p><strong>Example 1:</strong></p>
7-
8-
<pre>
9-
<strong>Input: </strong>&quot;aabcccccaaa&quot;
10-
<strong>Output: </strong>&quot;a2b1c5a3&quot;
11-
</pre>
12-
13-
<p><strong>Example 2:</strong></p>
14-
15-
<pre>
16-
<strong>Input: </strong>&quot;abbccd&quot;
17-
<strong>Output: </strong>&quot;abbccd&quot;
18-
<strong>Explanation: </strong>
19-
The compressed string is &quot;a1b2c2d1&quot;, which is longer than the original string.
20-
</pre>
21-
22-
<p>&nbsp;</p>
23-
24-
<p><strong>Note:</strong></p>
25-
26-
<ol>
27-
<li><code>0 &lt;= S.length &lt;= 50000</code></li>
28-
</ol>
29-
30-
31-
32-
## Solutions
33-
34-
35-
### Python3
36-
37-
```python
38-
39-
```
40-
41-
### Java
42-
43-
```java
44-
45-
```
46-
47-
### ...
48-
```
49-
50-
```
1+
# [01.06. Compress String](https://leetcode-cn.com/problems/compress-string-lcci)
2+
3+
## Description
4+
<p>Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would become a2blc5a3. If the &quot;compressed&quot; string would not become smaller than the original string, your method should return the original string. You can assume the string has only uppercase and lowercase letters (a - z).</p>
5+
6+
7+
8+
<p><strong>Example 1:</strong></p>
9+
10+
11+
12+
<pre>
13+
14+
<strong>Input: </strong>&quot;aabcccccaaa&quot;
15+
16+
<strong>Output: </strong>&quot;a2b1c5a3&quot;
17+
18+
</pre>
19+
20+
21+
22+
<p><strong>Example 2:</strong></p>
23+
24+
25+
26+
<pre>
27+
28+
<strong>Input: </strong>&quot;abbccd&quot;
29+
30+
<strong>Output: </strong>&quot;abbccd&quot;
31+
32+
<strong>Explanation: </strong>
33+
34+
The compressed string is &quot;a1b2c2d1&quot;, which is longer than the original string.
35+
36+
</pre>
37+
38+
<p>&nbsp;</p>
39+
40+
<p><strong>Note:</strong></p>
41+
42+
- `0 <= S.length <= 50000`
43+
44+
45+
46+
## Solutions
47+
48+
49+
### Python3
50+
51+
```python
52+
class Solution:
53+
def compressString(self, S: str) -> str:
54+
if len(S) < 2:
55+
return S
56+
p, q = 0, 1
57+
res = ''
58+
while q < len(S):
59+
if S[p] != S[q]:
60+
res += (S[p] + str(q - p))
61+
p = q
62+
q += 1
63+
res += (S[p] + str(q - p))
64+
return res if len(res) < len(S) else S
65+
```
66+
67+
### Java
68+
69+
```java
70+
class Solution {
71+
public String compressString(String S) {
72+
if (S == null || S.length() < 2) {
73+
return S;
74+
}
75+
char[] chars = S.toCharArray();
76+
int p = 0, q = 1, n = chars.length;
77+
StringBuilder sb = new StringBuilder();
78+
while (q < n) {
79+
if (chars[p] != chars[q]) {
80+
sb.append(chars[p]).append(q - p);
81+
p = q;
82+
}
83+
q += 1;
84+
}
85+
sb.append(chars[p]).append(q - p);
86+
String res = sb.toString();
87+
return res.length() < n ? res : S;
88+
}
89+
}
90+
```
91+
92+
### ...
93+
```
94+
95+
```
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public String compressString(String S) {
3+
if (S == null || S.length() < 2) {
4+
return S;
5+
}
6+
char[] chars = S.toCharArray();
7+
int p = 0, q = 1, n = chars.length;
8+
StringBuilder sb = new StringBuilder();
9+
while (q < n) {
10+
if (chars[p] != chars[q]) {
11+
sb.append(chars[p]).append(q - p);
12+
p = q;
13+
}
14+
q += 1;
15+
}
16+
sb.append(chars[p]).append(q - p);
17+
String res = sb.toString();
18+
return res.length() < n ? res : S;
19+
}
20+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def compressString(self, S: str) -> str:
3+
if len(S) < 2:
4+
return S
5+
p, q = 0, 1
6+
res = ''
7+
while q < len(S):
8+
if S[p] != S[q]:
9+
res += (S[p] + str(q - p))
10+
p = q
11+
q += 1
12+
res += (S[p] + str(q - p))
13+
return res if len(res) < len(S) else S

0 commit comments

Comments
 (0)