Created
April 5, 2022 01:39
-
-
Save BenDMyers/ae5f67a00f8afe8f2cb4953b71709a94 to your computer and use it in GitHub Desktop.
RWC: Equal With Deletions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Evaluate a string's results after backspaces and forward deletions. | |
* A `#` is a backspace, removing the previous undeleted character. | |
* A `%` is a forward deletion, removing the next letter. | |
* @param {string} str | |
* @returns {string} provided string, with deleted characters removed | |
*/ | |
function evaluateString(str) { | |
const characters = str.split(''); | |
let unresolvedForwardDeletionsRemaining = 0; | |
/** @type {string[]} */ | |
const outputCharacters = []; | |
for (const character of characters) { | |
if (character === '#') { | |
// Backspace character | |
outputCharacters.pop(); | |
} else if (character === '%') { | |
// "%" introduces a delete forward | |
unresolvedForwardDeletionsRemaining++; | |
} else if (unresolvedForwardDeletionsRemaining > 0) { | |
unresolvedForwardDeletionsRemaining--; | |
} else { | |
outputCharacters.push(character); | |
} | |
} | |
const output = outputCharacters.join(''); | |
return output; | |
} | |
/** | |
* Evaluates provided strings' deletions and determines ensuing equivalence. | |
* @param {string} firstString | |
* @param {string} secondString | |
*/ | |
function equalWithDeletions(firstString, secondString) { | |
const firstWithDeletions = evaluateString(firstString); | |
const secondWithDeletions = evaluateString(secondString); | |
console.log(firstWithDeletions === secondWithDeletions); | |
} | |
equalWithDeletions('a##x', '#a#x'); | |
equalWithDeletions('fi##f%%%th %%year #time###', 'fifth year time'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment