Skip to content

Commit 14b5836

Browse files
committed
feat: solve leetcode 0020
1 parent 3bb8f40 commit 14b5836

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const parens = new Map([
2+
['(', ')'],
3+
['[', ']'],
4+
['{', '}']
5+
])
6+
7+
/**
8+
* Unboxes a string option into a string.
9+
*
10+
* @param {string | undefined} str String option to be unboxed.
11+
* @returns {string} Unboxed string.
12+
*/
13+
const unboxOption = (str) => {
14+
if (str === undefined) {
15+
throw new Error('string is undefined')
16+
}
17+
18+
return str
19+
}
20+
21+
/**
22+
* Validates whether a string contains valid parentheses. Uses a stack.
23+
*
24+
* Time complexity: O(n)
25+
* Space complexity: O(n)
26+
*
27+
* @param {string} str String to be validated
28+
* @returns Whether the string contains valid parentheses.
29+
*/
30+
const hasValidParentheses = (str) => {
31+
// Empty string is valid
32+
if (str.length === 0) return true
33+
34+
// String of odd length can't be valid
35+
if (str.length % 2 !== 0) return false
36+
37+
/** @type {string[]} */
38+
const stack = []
39+
40+
for (let idx = 0; idx < str.length; idx++) {
41+
const currentChar = str[idx]
42+
43+
if (parens.has(currentChar)) {
44+
stack.push(unboxOption(parens.get(currentChar)))
45+
} else if (stack.pop() !== currentChar) {
46+
return false
47+
}
48+
}
49+
50+
// If the stack is empty, all parentheses have been closed.
51+
return stack.length === 0
52+
}
53+
54+
module.exports = {
55+
fun: hasValidParentheses,
56+
id: 'stack'
57+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const isParenthStack = require('./valid-parenth-stack')
2+
3+
const solutions = [
4+
isParenthStack
5+
]
6+
7+
module.exports = solutions
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const assert = require('node:assert/strict')
2+
const { describe, it } = require('node:test')
3+
4+
const solutions = require('./valid-parenth.repo')
5+
6+
for (const { fun, id } of solutions) {
7+
describe(`LeetCode 0020 'Valid Parentheses' solution '${id}'`, () => {
8+
it('returns true for empty string', () => {
9+
assert.equal(fun(''), true)
10+
})
11+
12+
it('returns false for string of size 1', () => {
13+
assert.equal(fun('['), false)
14+
})
15+
16+
it('identifies valid strings of length 2', () => {
17+
assert.equal(fun('()'), true)
18+
assert.equal(fun('[]'), true)
19+
assert.equal(fun('[]'), true)
20+
})
21+
22+
it('identifies invalid strings of length 2', () => {
23+
assert.equal(fun('(('), false)
24+
assert.equal(fun('(]'), false)
25+
assert.equal(fun(')('), false)
26+
})
27+
28+
it('returns false for strings of odd length', () => {
29+
assert.equal(fun('()('), false)
30+
assert.equal(fun('[)]()'), false)
31+
assert.equal(fun('([[()]]'), false)
32+
})
33+
34+
it('identifies valid string of length 4', () => {
35+
assert.equal(fun('(())'), true)
36+
assert.equal(fun('({})'), true)
37+
assert.equal(fun('()[]'), true)
38+
assert.equal(fun('()()'), true)
39+
})
40+
})
41+
}

0 commit comments

Comments
 (0)