-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy path3167-better-compression-of-string.js
More file actions
45 lines (39 loc) · 1.26 KB
/
3167-better-compression-of-string.js
File metadata and controls
45 lines (39 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* 3167. Better Compression of String
* https://leetcode.com/problems/better-compression-of-string/
* Difficulty: Medium
*
* You are given a string compressed representing a compressed version of a string. The format
* is a character followed by its frequency. For example, "a3b1a1c2" is a compressed version
* of the string "aaabacc".
*
* We seek a better compression with the following conditions:
* 1. Each character should appear only once in the compressed version.
* 2. The characters should be in alphabetical order.
*
* Return the better compression of compressed.
*
* Note: In the better version of compression, the order of letters may change, which is acceptable.
*/
/**
* @param {string} compressed
* @return {string}
*/
var betterCompression = function(compressed) {
const map = new Map();
let i = 0;
while (i < compressed.length) {
const char = compressed[i];
i++;
let frequency = '';
while (i < compressed.length && !isNaN(compressed[i])) {
frequency += compressed[i];
i++;
}
const count = parseInt(frequency);
map.set(char, (map.get(char) || 0) + count);
}
const sortedChars = [...map.keys()].sort();
const result = sortedChars.map(char => char + map.get(char)).join('');
return result;
};