Skip to content

Commit 0f953ae

Browse files
committedSep 22, 2021
Add solution #844
1 parent c257b9f commit 0f953ae

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
819|[Most Common Word](./0819-most-common-word.js)|Easy|
4949
824|[Goat Latin](./0824-goat-latin.js)|Easy|
5050
831|[Masking Personal Information](./0831-masking-personal-information.js)|Medium|
51+
844|[Backspace String Compare](./0844-backspace-string-compare.js)|Easy|
5152
867|[Transpose Matrix](./0867-transpose-matrix.js)|Easy|
5253
890|[Find and Replace Pattern](./0890-find-and-replace-pattern.js)|Medium|
5354
916|[Word Subsets](./0916-word-subsets.js)|Medium|
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* 844. Backspace String Compare
3+
* https://leetcode.com/problems/backspace-string-compare/
4+
* Difficulty: Easy
5+
*
6+
* Given two strings `s` and `t`, return true if they are equal when both
7+
* are typed into empty text editors. '#' means a backspace character.
8+
*
9+
* Note that after backspacing an empty text, the text will continue empty.
10+
*/
11+
12+
/**
13+
* @param {string} s
14+
* @param {string} t
15+
* @return {boolean}
16+
*/
17+
var backspaceCompare = function(s, t) {
18+
return handleBackspaces(s) === handleBackspaces(t);
19+
};
20+
21+
function handleBackspaces(input) {
22+
return input.split('').reduce((result, char) => {
23+
if (char === '#') {
24+
result.pop();
25+
} else {
26+
result.push(char);
27+
}
28+
return result;
29+
}, []).join('');
30+
}

0 commit comments

Comments
 (0)
Please sign in to comment.