-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ105.js
42 lines (31 loc) · 930 Bytes
/
Q105.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
// 2390 ----> Removing stars from a string
// You are given a string s, which contains stars *.
// In one operation, you can:
// Choose a star in s.
// Remove the closest non-star character to its left, as well as remove the star itself.
// Return the string after all stars have been removed.
// Note:
// The input will be generated such that the operation is always possible.
// It can be shown that the resulting string will always be unique.
var removeStars = function (s) {
s = s.split("");
while (s.includes("*")) {
let idx = s.indexOf("*");
// console.log(idx);
for (let i = idx; i >= 0; i--) {
s.splice(idx, 1);
if (s[i] !== "*") {
console.log(s);
// delete s[i]
// delete s[idx]
s.splice(i, 1);
break;
}
}
}
s = s.join("");
return s;
};
let s = "leet**cod*e";
console.log(removeStars(s));
// console.log(s.indexOf("b"));