MCPcopy Create free account
hub / github.com/Blankj/awesome-java-leetcode / isValid

Method isValid

src/com/blankj/easy/_0020/Solution.java:12–27  ·  view source on GitHub ↗
(String s)

Source from the content-addressed store, hash-verified

10 */
11public class Solution {
12 public boolean isValid(String s) {
13 char[] stack = new char[s.length() + 1];
14 int top = 1;
15 for (char c : s.toCharArray()) {
16 if (c == '(' || c == '[' || c == '{') {
17 stack[top++] = c;
18 } else if (c == ')' && stack[--top] != '(') {
19 return false;
20 } else if (c == ']' && stack[--top] != '[') {
21 return false;
22 } else if (c == '}' && stack[--top] != '{') {
23 return false;
24 }
25 }
26 return top == 1;
27 }
28
29 public static void main(String[] args) {
30 Solution solution = new Solution();

Callers 1

mainMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected