Skip to content

Commit 1418465

Browse files
author
Tushar Borole
committed
443. String Compression
1 parent e76e577 commit 1418465

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed

.idea/workspace.xml

Lines changed: 15 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
| # | Title | Solution | Runtime | Memory | Difficulty | Time Complexity | Space Complexity | Explanation |
99
|:-----|:--------------------------------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------|:-----------------|:---------------------|:-----------|:----------------|:-----------------|:------------------------------------------------------------------|
1010
| 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case/) | [Solution](to_lower_case.js) | 72 ms | 32.1 MB | Easy | | | |
11+
| 443 | [String Compression](https://leetcode.com/problems/string-compression/) | [string_compression.js](string_compression.js) | | | Easy | | | |
1112
| 122 | [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) | [iterative](best_time_to_buy_and_sell_stock_II.js) | 56 ms | 35.4 MB | Easy | O(N) | O(1) | |
1213
| 1122 | [Relative Sort Array](https://leetcode.com/problems/relative-sort-array/) | [Ittrative](relative_sort_array.js) | 52 ms | 34.9 MB | Easy | | | |
1314
| 67 | [Add Binary](https://leetcode.com/problems/add-binary/) | [addBinary.js](addBinary.js) | 72 ms | 35.7 MB | Easy | | | [:link:](https://www.youtube.com/watch?v=6axItxXHouA) |

string_compression.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {character[]} chars
3+
* @return {number}
4+
*/
5+
var compress = function(chars) {
6+
chars.push("end");
7+
let count = 1;
8+
for (var i = 0; i < chars.indexOf("end"); i++) {
9+
if (chars[i] === chars[i + 1]) {
10+
count++;
11+
continue;
12+
}
13+
14+
chars.push(chars[i]);
15+
if (count !== 1) {
16+
chars.push(...count.toString().split(""));
17+
}
18+
19+
count = 1;
20+
}
21+
22+
chars.splice(0, chars.indexOf("end") + 1);
23+
};

0 commit comments

Comments
 (0)