Skip to content

Commit 1a22aa0

Browse files
committed
feat: write alternative solution
1 parent 8eff578 commit 1a22aa0

File tree

6 files changed

+118
-24
lines changed

6 files changed

+118
-24
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Valid Parentheses
2+
3+
Given a string `str` containing just the characters `'('`, `')'`, `'['`, `']'`,
4+
`'{'`, `'}'`, determine if the string is valid.
5+
6+
* Open brackets must be closed by the same type of brackets.
7+
* Open brackets must be closed in the correct order.
8+
* Every close bracket has a corresponding open bracket of the same type.
9+
* Empty strings are valid strings.
10+
11+
## Function Signature
12+
13+
```typescript
14+
function hasValidParentheses(str: string): boolean
15+
```
16+
17+
## Expected Behavior
18+
19+
```
20+
In: ''
21+
Out: true
22+
23+
In: '('
24+
Out: false
25+
26+
In: '()'
27+
Out: true
28+
29+
In: '(]'
30+
Out: false
31+
32+
In: '()[]{}'
33+
Out: true
34+
35+
In: '[{()}]'
36+
Out: true
37+
38+
In: '([)'
39+
Out: false
40+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const adjacentParens = /\(\)|\{\}|\[\]/g
2+
3+
/**
4+
* Validates whether a string contains valid parentheses. Uses the 'replace'
5+
* string method.
6+
*
7+
* Time complexity: O(n^2)
8+
* Space complexity: O(n)
9+
*
10+
* @param {string} str String to be validated.
11+
* @returns {boolean} Whether the string contains valid parentheses.
12+
*/
13+
const hasValidParentheses = (str) => {
14+
if (str.length === 0) return true
15+
else if (str.length % 2 !== 0) return false
16+
17+
let currentStr = str
18+
let prevLength
19+
20+
// Remove adjacent pairs until there are none (until string size doesn't
21+
// change)
22+
do {
23+
prevLength = currentStr.length
24+
currentStr = currentStr.replace(adjacentParens, '')
25+
} while (currentStr.length < prevLength)
26+
27+
return currentStr.length === 0
28+
}
29+
30+
module.exports = {
31+
fun: hasValidParentheses,
32+
id: 'string-remove'
33+
}

src/leet-code/0020-valid-parentheses/valid-parenth-stack.js

+6-21
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,27 @@ const parens = new Map([
55
['{', '}']
66
])
77

8-
/**
9-
* Unboxes a string option into a string.
10-
*
11-
* @param {string | undefined} str String option to be unboxed.
12-
* @returns {string} Unboxed string.
13-
*/
14-
const unboxOption = (str) => {
15-
if (str === undefined) throw new Error('string is undefined')
16-
return str
17-
}
18-
198
/**
209
* Validates whether a string contains valid parentheses. Uses a stack.
2110
*
2211
* Time complexity: O(n)
2312
* Space complexity: O(n)
2413
*
2514
* @param {string} str String to be validated
26-
* @returns Whether the string contains valid parentheses.
15+
* @returns {boolean} Whether the string contains valid parentheses.
2716
*/
2817
const hasValidParentheses = (str) => {
29-
// Empty string is valid
3018
if (str.length === 0) return true
31-
32-
// String of odd length can't be valid
3319
if (str.length % 2 !== 0) return false
3420

3521
/** @type {string[]} */
3622
const stack = []
3723

38-
for (let idx = 0; idx < str.length; idx++) {
39-
const currentChar = str[idx]
40-
41-
if (parens.has(currentChar)) {
42-
stack.push(unboxOption(parens.get(currentChar)))
43-
} else if (stack.pop() !== currentChar) {
24+
for (const char of str) {
25+
if (parens.has(char)) {
26+
const parChar = parens.get(char)
27+
if (parChar !== undefined) stack.push(parChar)
28+
} else if (stack.pop() !== char) {
4429
return false
4530
}
4631
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const isParenthStack = require('./valid-parenth-stack')
2+
const isParenthReplace = require('./valid-parenth-replace')
23

34
const solutions = [
4-
isParenthStack
5+
isParenthStack,
6+
isParenthReplace
57
]
68

79
module.exports = solutions

src/leet-code/0020-valid-parentheses/valid-parenth.test.js

+26-2
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,33 @@ for (const { fun, id } of solutions) {
3333

3434
it('identifies valid string of length 4', () => {
3535
assert.equal(fun('(())'), true)
36-
assert.equal(fun('({})'), true)
36+
assert.equal(fun('[[]]'), true)
37+
assert.equal(fun('{{}}'), true)
38+
assert.equal(fun('([])'), true)
39+
assert.equal(fun('[][]'), true)
3740
assert.equal(fun('()[]'), true)
38-
assert.equal(fun('()()'), true)
41+
assert.equal(fun('{}()'), true)
42+
})
43+
44+
it('identifies invalid string of length 4', () => {
45+
assert.equal(fun('(((('), false)
46+
assert.equal(fun('))))'), false)
47+
assert.equal(fun('{{})'), false)
48+
assert.equal(fun('())('), false)
49+
assert.equal(fun('][[]'), false)
50+
})
51+
52+
it('identifies valid string of length 6', () => {
53+
assert.equal(fun('((()))'), true)
54+
assert.equal(fun('[[[]]]'), true)
55+
assert.equal(fun('{{{}}}'), true)
56+
})
57+
58+
it('identifies valid large string', () => {
59+
assert.equal(
60+
fun('({()}[{}]([]))[{{}}]{}((()))'),
61+
true
62+
)
3963
})
4064
})
4165
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const { timeAndReport } = require('../../../lib/time')
2+
3+
const solutions = require('./valid-parenth.repo')
4+
5+
const parensStr = '({()}[{}]([]))[{{}}]{}((()))'
6+
const args = [parensStr]
7+
const runs = 100_000
8+
const id = 'LeetCode 0020 "Valid Parentheses"'
9+
10+
timeAndReport(solutions, args, runs, id)

0 commit comments

Comments
 (0)