Skip to content

Commit 5e491ed

Browse files
committed
solve problem String Compression
1 parent b61f152 commit 5e491ed

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ All solutions will be accepted!
181181
|28|[Implement Strstr](https://leetcode-cn.com/problems/implement-strstr/description/)|[java/py/js](./algorithms/ImplementStrstr)|Easy|
182182
|459|[Repeated Substring Pattern](https://leetcode-cn.com/problems/repeated-substring-pattern/description/)|[java/py/js](./algorithms/RepeatedSubstringPattern)|Easy|
183183
|581|[Shortest Unsorted Continuous Subarray](https://leetcode-cn.com/problems/shortest-unsorted-continuous-subarray/description/)|[java/py/js](./algorithms/ShortestUnsortedContinuousSubarray)|Easy|
184+
|443|[String Compression](https://leetcode-cn.com/problems/string-compression/description/)|[java/py/js](./algorithms/StringCompression)|Easy|
184185

185186
# Database
186187
|#|Title|Solution|Difficulty|
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# String Compression
2+
This problem is easy to solve
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution {
2+
public int compress(char[] chars) {
3+
Character ch = null;
4+
int count = 0,
5+
length = 0;
6+
7+
for (char c : chars) {
8+
if (ch == null) {
9+
count = 1;
10+
ch = c;
11+
} else if (ch == c) {
12+
count++;
13+
} else if (ch != c) {
14+
chars[length] = ch;
15+
length++;
16+
17+
if (count > 1) {
18+
String countStr = String.valueOf(count);
19+
for (int j = 0; j < countStr.length(); j++) {
20+
chars[length] = countStr.charAt(j);
21+
length++;
22+
}
23+
}
24+
25+
count = 1;
26+
ch = c;
27+
}
28+
}
29+
30+
chars[length] = ch;
31+
length++;
32+
33+
if (count > 1) {
34+
String countStr = String.valueOf(count);
35+
for (int j = 0; j < countStr.length(); j++) {
36+
chars[length] = countStr.charAt(j);
37+
length++;
38+
}
39+
}
40+
41+
return length;
42+
}
43+
}
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @param {character[]} chars
3+
* @return {number}
4+
*/
5+
var compress = function(chars) {
6+
let char = null,
7+
count = 0,
8+
length = 0
9+
10+
chars.forEach(c => {
11+
if (char === null) {
12+
count = 1
13+
char = c
14+
} else if (char === c) {
15+
count++
16+
} else if (char !== c) {
17+
chars[length] = char
18+
length++
19+
20+
if (count > 1) {
21+
let countStr = String(count)
22+
for (let j = 0; j < countStr.length; j++) {
23+
chars[length] = countStr[j]
24+
length++
25+
}
26+
}
27+
28+
count = 1
29+
char = c
30+
}
31+
})
32+
33+
chars[length] = char
34+
length++
35+
36+
if (count > 1) {
37+
let countStr = String(count)
38+
for (let j = 0; j < countStr.length; j++) {
39+
chars[length] = countStr[j]
40+
length++
41+
}
42+
}
43+
44+
return length
45+
};
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution(object):
2+
def compress(self, chars):
3+
"""
4+
:type chars: List[str]
5+
:rtype: int
6+
"""
7+
char = None
8+
count = 0
9+
length = 0
10+
11+
for c in chars:
12+
if char == None:
13+
count = 1
14+
char = c
15+
elif char == c:
16+
count += 1
17+
elif char != c:
18+
chars[length] = char
19+
length += 1
20+
21+
if count > 1:
22+
count_str = str(count)
23+
for c in count_str:
24+
chars[length] = c
25+
length += 1
26+
27+
count = 1
28+
char = c
29+
30+
chars[length] = char
31+
length += 1
32+
33+
if count > 1:
34+
count_str = str(count)
35+
for c in count_str:
36+
chars[length] = c
37+
length += 1
38+
39+
return length
40+
41+

0 commit comments

Comments
 (0)