File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed
Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * Runtime: 4ms
3+ * Time Complexity: O(n)
4+ *
5+ * Memory: 42.94MB
6+ * Space Complexity: O(n)
7+ *
8+ * Approach: ์คํ ์ฌ์ฉ
9+ * - ์ด๋ฆฐ ๊ดํธ๋ฅผ ํธ์ํ๋ฉด์ ๋ซํ ๊ดํธ๊ฐ ๋์ฌ ๋๋ง๋ค ์คํ์์ ํํ์ฌ ์ง์ด ๋ง๋์ง ํ์ธ
10+ */
11+ class Solution {
12+ public boolean isValid (String s ) {
13+ if (s .length ()%2 == 1 ) return false ;
14+
15+ Stack <Character > stack = new Stack <>();
16+ Map <Character , Character > map = new HashMap <>();
17+ map .put ('(' , ')' );
18+ map .put ('{' , '}' );
19+ map .put ('[' , ']' );
20+
21+ for (char ch : s .toCharArray ()) {
22+ if (map .containsKey (ch )) {
23+ stack .push (ch );
24+ } else if (")}]" .indexOf (ch ) != -1 ) {
25+ if (stack .isEmpty () || ch != map .get (stack .pop ())) {
26+ return false ;
27+ }
28+ }
29+ }
30+
31+ return stack .isEmpty ();
32+ }
33+ }
You canโt perform that action at this time.
0 commit comments