Added test cases for LongestValidParentheses

This commit is contained in:
Omkarnath Parida
2021-10-03 20:58:53 +05:30
parent c2b3931391
commit db3caea70f
2 changed files with 20 additions and 9 deletions

View File

@ -5,7 +5,7 @@
find the length of the longest valid (well-formed) parentheses substring.
*/
const longestValidParentheses = (s) => {
export const longestValidParentheses = (s) => {
const n = s.length
const stack = []
@ -33,11 +33,3 @@ const longestValidParentheses = (s) => {
res.push(0)
return Math.max(...res)
}
const main = () => {
console.log(longestValidParentheses(')()())')) // output -> 4
console.log(longestValidParentheses('')) // output -> 0
console.log(longestValidParentheses('(()')) // output -> 2
}
main()

View File

@ -0,0 +1,19 @@
import { longestValidParentheses } from '../LongestValidParentheses'
describe('longestValidParentheses', () => {
it('expects to return 0 as longest valid parentheses substring', () => {
expect(longestValidParentheses('')).toBe(0)
})
it('expects to return 2 as longest valid parentheses substring', () => {
expect(longestValidParentheses('(()')).toBe(2)
})
it('expects to return 2 as longest valid parentheses substring', () => {
expect(longestValidParentheses(')()())')).toBe(4)
})
it('expects to return 2 as longest valid parentheses substring', () => {
expect(longestValidParentheses('(((')).toBe(0)
})
})