-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstructy-006-uncompress.js
44 lines (36 loc) · 1.1 KB
/
structy-006-uncompress.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
32
33
34
35
36
37
38
39
40
41
42
43
44
// https://structy.net/problems/premium/uncompress
// p: str
// r: str
// e: uncompress("2c3a1t"); // -> 'ccaaat'
// two pointers, using array for space complexity bcuz string concat creates another string
const uncompress = (s) => {
let result = [];
let indexOfLastChar = 0;
for (let i = 0; i < s.length; i++) {
let char = s[i];
let times = s.slice(indexOfLastChar, i);
if (char.toLowerCase() !== char.toUpperCase()) {
result.push(char.repeat(times));
indexOfLastChar = i + 1;
}
}
console.log(result.join(""));
return result.join("");
};
// uncompress("2c3a1t");
uncompress("3n12e2z");
// const uncompress = (s) => {
// let result = "";
// let indexOfLastChar = 0;
// for (let i = 0; i < s.length; i++) {
// let char = s[i];
// let times = s.slice(indexOfLastChar, i);
// console.log(indexOfLastChar, times);
// if (char.toLowerCase() !== char.toUpperCase()) {
// result = result + char.repeat(times);
// indexOfLastChar = i + 1;
// }
// }
// console.log(result);
// return result;
// };