Skip to content

Commit 01da5d1

Browse files
authored
Create Solution.js
1 parent c66687a commit 01da5d1

File tree

1 file changed

+22
-0
lines changed
  • solution/3100-3199/3163.String Compression III

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {string} word
3+
* @return {string}
4+
*/
5+
var compressedString = function(word) {
6+
const ans = [];
7+
const n = word.length;
8+
for (let i = 0; i < n;) {
9+
let j = i + 1;
10+
while (j < n && word[j] === word[i]) {
11+
++j;
12+
}
13+
let k = j - i;
14+
while (k) {
15+
const x = Math.min(k, 9);
16+
ans.push(x + word[i]);
17+
k -= x;
18+
}
19+
i = j;
20+
}
21+
return ans.join('');
22+
};

0 commit comments

Comments
 (0)