-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringCompressionIII.ts
More file actions
39 lines (31 loc) · 1.14 KB
/
stringCompressionIII.ts
File metadata and controls
39 lines (31 loc) · 1.14 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
// First Approach - Iterating over word counting the actual char and it's count, and whenever actual char is different than the iteration char, record count and actual char into answer string. (37ms - Beats 55.56%)
function compressedString(word: string): string {
let ans = '';
let counter = 0, actualChar: string = word[0];
for(let i = 0; i <= word.length; i++) {
if(counter === 9 || word[i] !== actualChar) {
ans += counter + actualChar;
counter = 0;
actualChar = word[i];
}
counter++;
}
return ans;
};
// Second Approach - Same logic of first approach but comparing numbers with their successors, and if it's different record into answer string. (34ms - Beats 55.56%)
function compressedString(word: string): string {
let ans = '';
let counter = 1;
for(let i = 0; i <= word.length; i++) {
if(counter === 9 || word[i] !== word[i + 1]) {
ans += counter + word[i];
counter = 0;
}
counter++;
}
return ans;
};
const case1 = compressedString("abcde");
const case2 = compressedString("aaaaaaaaaaaaaabb");
console.log(`case1: ${case1} // ${case1 === "1a1b1c1d1e"}`);
console.log(`case2: ${case2} // ${case2 === "9a5a2b"}`);