-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathstringCompression.js
More file actions
36 lines (25 loc) · 989 Bytes
/
stringCompression.js
File metadata and controls
36 lines (25 loc) · 989 Bytes
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
// 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).
const stringCompression = (str) => {
let arr = [];
// let count =[];
str.split('').forEach((letter) => {
if (str.split('').includes(letter) && !arr.includes(letter)) {
arr.push(letter);
}
// Now, if I have the letters, use RegExp or match to get the count
});
return arr.join('').replace(str);
}
console.log(stringCompression('aaabbccc'));
const stringSearch = (str) => {
for (let count =- 1, index =- 2; index != 1;
count++, index = str.indexOf(stringSearch, index + 1)
);
}
return str;
console.log(stringSearch('aaabbb'));