-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from purnasth/backspace/solution
feat: Backspace String Compare Problem JavaScript
- Loading branch information
Showing
2 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Backspace String Compare</title> | ||
</head> | ||
<body> | ||
<button onclick="showCustomDialog()">Compare Strings</button> | ||
|
||
<div id="custom-dialog" style="display: none"> | ||
<p id="result-text">Result will be displayed here</p> | ||
</div> | ||
|
||
<script> | ||
function showCustomDialog() { | ||
const customDialog = document.getElementById("custom-dialog"); | ||
const resultText = document.getElementById("result-text"); | ||
|
||
const s = prompt("Enter the first string:"); | ||
const t = prompt("Enter the second string:"); | ||
const result = backspaceCompare(s, t); | ||
|
||
customDialog.style.display = "block"; | ||
|
||
if (result) { | ||
resultText.innerHTML = `Are the first input "${s}" and the second input "${t}" equal after considering backspaces?<br><span style="color: green;">True</span>`; | ||
} else { | ||
resultText.innerHTML = `Are the first input "${s}" and the second input "${t}" equal after considering backspaces?<br><span style="color: red;">False</span>`; | ||
} | ||
} | ||
</script> | ||
<script src="./solution.js"></script> | ||
</body> | ||
</html> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Backspace String Compare Logic Starts Here | ||
const backspaceCompare = (s, t) => { | ||
const getNextValidCharIndex = (str, index) => { | ||
let backspaceCount = 0; | ||
while (index >= 0) { | ||
if (str[index] === "#") { | ||
backspaceCount++; | ||
index--; | ||
} else if (backspaceCount > 0) { | ||
backspaceCount--; | ||
index--; | ||
} else { | ||
break; | ||
} | ||
} | ||
return index; | ||
}; | ||
|
||
let sPointer = s.length - 1; | ||
let tPointer = t.length - 1; | ||
|
||
while (sPointer >= 0 || tPointer >= 0) { | ||
sPointer = getNextValidCharIndex(s, sPointer); | ||
tPointer = getNextValidCharIndex(t, tPointer); | ||
|
||
if (sPointer < 0 && tPointer < 0) { | ||
return true; | ||
} | ||
|
||
if (sPointer < 0 || tPointer < 0 || s[sPointer] !== t[tPointer]) { | ||
return false; | ||
} | ||
|
||
sPointer--; | ||
tPointer--; | ||
} | ||
|
||
return true; | ||
}; |