A, JS API
Math.abs(); // Get the absolute value
String.prototype.charCodeAt() // Get the character ASCII code value
Copy the code
The ASCII code value of a character
'a'.charCodeAt() / / 97
'A'.charCodeAt() / / 65
'b'.charCodeAt() / / 98
'B'.charCodeAt() / / 66
Copy the code
∴ a (1) when s[I] and S [i-1] are the same letter, and only one of them is ∴ B (2) :
Math.abs(s[i].charCodeAt()-s[i-1].charCodeAt())===32
Copy the code
The team included in the stack
var makeGood = function (str) {
// Queue q holds the input and stack S holds the output
let q = str.split(' '),s = [];
// Iterate over the queue
while (q.length) {
// Queue character
let curS = q.shift()
// Queue characters ASCII
let curV = curS.charCodeAt()
// stack end character ASCII
let preV = s[s.length - 1] && s[s.length - 1].charCodeAt()
// If the queue character and the stack end character meet the requirements of the question, it needs to be deleted
if (preV && Math.abs(curV - preV) === 32) {
/ / out of the stack
s.pop()
} else {
/ / into the stack
s.push(curS)
}
}
/ / output
return s.join(' ')}Copy the code