Skip to content

Commit 214defd

Browse files
authored
Create 3561-resulting-string-after-adjacent-removals.js
1 parent 364d797 commit 214defd

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {string} s
3+
* @return {string}
4+
*/
5+
var resultingString = function (s) {
6+
let stack = []
7+
for (let c of s) {
8+
if (stack.length === 0) {
9+
stack.push(c)
10+
} else {
11+
if (
12+
(c === 'z' && stack[stack.length - 1] === 'a') ||
13+
(c === 'a' && stack[stack.length - 1] === 'z') ||
14+
Math.abs(c.charCodeAt(0) - stack[stack.length - 1].charCodeAt(0)) === 1
15+
) {
16+
stack.pop()
17+
} else {
18+
stack.push(c)
19+
}
20+
}
21+
}
22+
return stack.join('')
23+
}

0 commit comments

Comments
 (0)