Skip to content

Commit 0e788f6

Browse files
committed
Backspace String Compare
1 parent 3dbc933 commit 0e788f6

File tree

7 files changed

+88
-33
lines changed

7 files changed

+88
-33
lines changed

README.MD

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@ Solutions to problems from the [LeetCode Patterns](https://seanprashad.com/leetc
44

55
## Problems
66

7-
| Title | Solution | Difficulty |
8-
|-------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------|------------|
9-
| [217. Contains Duplicate](https://leetcode.com/problems/contains-duplicate) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/containsDuplicate/index.js) | Easy |
10-
| [268. Missing Number](https://leetcode.com/problems/missing-number) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/tree/main/solutions/missingNumber/index.js) | Easy |
11-
| [448. Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/findDisappearedNumbers/index.js) | Easy |
12-
| [136. Single Number](https://leetcode.com/problems/single-number) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/singleNumber/index.js) | Easy |
13-
| [70. Climbing Stairs](https://leetcode.com/problems/climbing-stairs) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/climbStairs/index.js) | Easy |
14-
| [121. Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/maxProfit/index.js) | Easy |
15-
| [303. Range Sum Query - Immutable](https://leetcode.com/problems/range-sum-query-immutable) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/sumRange/index.js) | Easy |
16-
| [338. Counting Bits](https://leetcode.com/problems/counting-bits) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/countBits/index.js) | Easy |
17-
| [141. Linked List Cycle](https://leetcode.com/problems/linked-list-cycle) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/hasCycle/index.js) | Easy |
18-
| [876. Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/middleNode/index.js) | Easy |
19-
| [206. Reverse Linked List](https://leetcode.com/problems/reverse-linked-list) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/reverseList/index.js) | Easy |
20-
| [234. Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/isPalindrome/index.js) | Easy |
21-
| [203. Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/removeElements/index.js) | Easy |
22-
| [1. Two Sum](https://leetcode.com/problems/two-sum) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/twoSum/index.js) | Easy |
7+
| Title | Solution | Difficulty |
8+
|--------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------|------------|
9+
| [Contains Duplicate](https://leetcode.com/problems/contains-duplicate) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/containsDuplicate/index.js) | Easy |
10+
| [Missing Number](https://leetcode.com/problems/missing-number) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/tree/main/solutions/missingNumber/index.js) | Easy |
11+
| [Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/findDisappearedNumbers/index.js) | Easy |
12+
| [Single Number](https://leetcode.com/problems/single-number) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/singleNumber/index.js) | Easy |
13+
| [Climbing Stairs](https://leetcode.com/problems/climbing-stairs) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/climbStairs/index.js) | Easy |
14+
| [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/maxProfit/index.js) | Easy |
15+
| [Range Sum Query - Immutable](https://leetcode.com/problems/range-sum-query-immutable) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/sumRange/index.js) | Easy |
16+
| [Counting Bits](https://leetcode.com/problems/counting-bits) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/countBits/index.js) | Easy |
17+
| [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/hasCycle/index.js) | Easy |
18+
| [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/middleNode/index.js) | Easy |
19+
| [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/reverseList/index.js) | Easy |
20+
| [Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/isPalindrome/index.js) | Easy |
21+
| [Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/removeElements/index.js) | Easy |
22+
| [Two Sum](https://leetcode.com/problems/two-sum) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/twoSum/index.js) | Easy |
23+
| [Backspace String Compare](https://leetcode.com/problems/backspace-string-compare) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/backspaceCompare/index.js) | Easy |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## [844. Backspace String Compare](https://leetcode.com/problems/backspace-string-compare)
2+
3+
Given two strings `s` and `t`, return `true` if they are equal when both are typed into empty text editors. `'#'` means a backspace character.
4+
5+
Note that after backspacing an empty text, the text will continue empty.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {string} str
3+
* @return {string}
4+
*/
5+
function modify(str) {
6+
let backspaces = 0,
7+
result = '';
8+
9+
for (let i = str.length - 1; i >= 0; i--) {
10+
if (str[i] === '#') {
11+
backspaces++;
12+
} else if (backspaces > 0) {
13+
backspaces--;
14+
} else {
15+
result = str[i] + result;
16+
}
17+
}
18+
19+
return result;
20+
}
21+
22+
/**
23+
* @param {string} s
24+
* @param {string} t
25+
* @return {boolean}
26+
*/
27+
function backspaceCompare(s, t) {
28+
return modify(s) === modify(t);
29+
}
30+
31+
module.exports = backspaceCompare;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const backspaceCompare = require('.');
2+
3+
describe('Backspace String Compare', () => {
4+
it('Input: s = "ab#c", t = "ad#c"', () => {
5+
const result = backspaceCompare('ab#c', 'ad#c');
6+
const expected = true;
7+
expect(result).toStrictEqual(expected);
8+
});
9+
10+
it('Input: s = "ab##", t = "c#d#"', () => {
11+
const result = backspaceCompare('ab##', 'c#d#');
12+
const expected = true;
13+
expect(result).toStrictEqual(expected);
14+
});
15+
16+
it('Input: s = "a#c", t = "b"', () => {
17+
const result = backspaceCompare('a#c', 'b');
18+
const expected = false;
19+
expect(result).toStrictEqual(expected);
20+
});
21+
});

solutions/findDisappearedNumbers/index.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33
* @return {number[]}
44
*/
55
function findDisappearedNumbers(nums) {
6-
const
7-
set = new Set(nums),
8-
result = [];
6+
const set = new Set(nums),
7+
result = [];
98

109
for (let i = 1; i <= nums.length; i++) {
1110
if (!set.has(i)) {
12-
result.push(i)
11+
result.push(i);
1312
}
1413
}
1514

1615
return result;
1716
}
1817

19-
module.exports = findDisappearedNumbers;
18+
module.exports = findDisappearedNumbers;

solutions/isPalindrome/index.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,17 @@ function isPalindrome(head) {
77
return false;
88
}
99

10-
let
11-
result = '',
12-
reverseResult = '',
13-
node = head;
10+
let result = '',
11+
reverseResult = '',
12+
node = head;
1413

1514
while (node) {
16-
result = `${result}${node.val}`
17-
reverseResult = `${node.val}${reverseResult}`
18-
node = node.next
15+
result = `${result}${node.val}`;
16+
reverseResult = `${node.val}${reverseResult}`;
17+
node = node.next;
1918
}
2019

21-
return result === reverseResult
20+
return result === reverseResult;
2221
}
2322

2423
module.exports = isPalindrome;

solutions/maxProfit/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
* @return {number}
44
*/
55
function maxProfit(prices) {
6-
let
7-
min = prices[0],
8-
max = 0;
6+
let min = prices[0],
7+
max = 0;
98

109
for (let i = 1; i < prices.length; i++) {
1110
min = Math.min(min, prices[i]);
@@ -15,4 +14,4 @@ function maxProfit(prices) {
1514
return max;
1615
}
1716

18-
module.exports = maxProfit;
17+
module.exports = maxProfit;

0 commit comments

Comments
 (0)