-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringCompression.js
31 lines (24 loc) · 966 Bytes
/
stringCompression.js
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
// String Compression: Implement a method to perform basic string
// compression using the counts of repeated characters.For example,
// the string aabcccccaaa would become a2b1c5a3.If the "compressed"
// 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).
function compression(string) {
const letters = string.split("");
const letterCountMap = new Map();
for (letter of letters) {
if (letterCountMap.has(letter)) {
letterCountMap.set(letter, letterCountMap.get(letter) + 1);
} else {
letterCountMap.set(letter, 1);
}
}
let compressedString = "";
for (let [key, value] of letterCountMap) {
compressedString = compressedString + key + value;
}
console.log(compressedString);
return compressedString.length < string.length ? compressedString : string;
}
compression("aabcccccaaa");