Skip to content

Commit

Permalink
#20 resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
yukisato committed Dec 13, 2019
1 parent eecbc16 commit e46181d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
23 changes: 23 additions & 0 deletions leetcode/solutions/0020.ValidParentheses/isValid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @param {string} s
* @return {boolean}
*/
export default function isValid(
s: string
): boolean {
if (s.length % 2 !== 0) {
return false;
}

const closerMap = new Map([["{", "}"], ["(", ")"], ["[", "]"]]);
const openerSet = new Set(closerMap.keys());
const closerStack: string[] = [];
for (let i = 0; i < s.length; i++) {
if (openerSet.has(s[i])) {
closerStack.push(closerMap.get(s[i])!);
} else if (closerStack.pop() !== s[i]) {
return false;
}
}
return closerStack.length === 0;
}
11 changes: 11 additions & 0 deletions leetcode/solutions/0020.ValidParentheses/isValid_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { test } from "https://deno.land/std/testing/mod.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
import isValid from "./isValid.ts";

test("0020. Valid Parentheses", () => {
assertEquals(isValid("()"), true);
assertEquals(isValid("()[]{}"), true);
assertEquals(isValid("(]"), false);
assertEquals(isValid("([)]"), false);
assertEquals(isValid("{[]}"), true);
});

0 comments on commit e46181d

Please sign in to comment.