Skip to content

Commit e22405b

Browse files
refactor 20
1 parent bd16189 commit e22405b

File tree

1 file changed

+30
-5
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+30
-5
lines changed

src/main/java/com/fishercoder/solutions/_20.java

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,43 @@
11
package com.fishercoder.solutions;
22

3-
import java.util.ArrayDeque;
4-
import java.util.Deque;
3+
import java.util.Stack;
54

65
/**
76
* 20. Valid Parentheses
87
*
98
* Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
10-
* The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.*/
9+
*
10+
* An input string is valid if:
11+
*
12+
* 1. Open brackets must be closed by the same type of brackets.
13+
* 2. Open brackets must be closed in the correct order.
14+
*
15+
* Note that an empty string is also considered valid.
16+
*
17+
* Example 1:
18+
* Input: "()"
19+
* Output: true
20+
*
21+
* Example 2:
22+
* Input: "()[]{}"
23+
* Output: true
24+
*
25+
* Example 3:
26+
* Input: "(]"
27+
* Output: false
28+
*
29+
* Example 4:
30+
* Input: "([)]"
31+
* Output: false
32+
*
33+
* Example 5:
34+
* Input: "{[]}"
35+
* Output: true
36+
* */
1137
public class _20 {
12-
1338
public static class Solution1 {
1439
public boolean isValid(String s) {
15-
Deque<Character> stack = new ArrayDeque<>();
40+
Stack<Character> stack = new Stack<>();
1641
for (int i = 0; i < s.length(); i++) {
1742
if (s.charAt(i) == '(' || s.charAt(i) == '{' || s.charAt(i) == '[') {
1843
stack.push(s.charAt(i));

0 commit comments

Comments
 (0)