Skip to content

Instantly share code, notes, and snippets.

@blacksheep557
Created September 28, 2021 06:34
Show Gist options
  • Select an option

  • Save blacksheep557/8ff9cc742b112496846a43f093bbf006 to your computer and use it in GitHub Desktop.

Select an option

Save blacksheep557/8ff9cc742b112496846a43f093bbf006 to your computer and use it in GitHub Desktop.
function decipherThis(string) {
const words = string.split(' ');
return words.map(word => decode(word.split(''))).join(' ');
}
function decode(wordArr) {
let hex = ''
let secChar
for (let [idx, char] of wordArr.entries()) {
if (Number(char) || char == 0) {
hex += char
} else {
secChar = idx
break;
}
}
let res = String.fromCharCode(hex)
res += wordArr.length > hex.length ? wordArr.pop() : ''
res += wordArr.length ? wordArr.splice(secChar + 1).join('') : ''
res += wordArr[secChar] ? wordArr[secChar] : ''
return res
}
function backSpace(string) {
const resStack = []
for (const char of string) {
if (char === '#') resStack.length && resStack.pop()
else resStack.push(char)
}
return resStack.join('')
}
console.log(backSpace("abc#d##c"))// ==> "ac"
console.log(backSpace("abc##d######"))// ==> "ac"
console.log(backSpace("#######"))// ==> "ac"
console.log(backSpace(""))// ==> "ac"
// "abc##d######" ==> ""
// "#######" ==> ""
// "" ==> ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment