MCPcopy Create free account
hub / github.com/Sugapriyan-P-K/blind75 / Solution

Class Solution

String/56validParentheses/validParentheses.java:2–25  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1
2class Solution {
3 private static boolean valid(String s) {
4 List<Character> stack = new ArrayList<Character>();
5 for (int i = 0; i < s.length(); i++) {
6 if (s.charAt(i) == '{' || s.charAt(i) == '[' || s.charAt(i) == '(') {
7 stack.add(s.charAt(i));
8 } else if (stack.size() > 0 && ((stack.get(stack.size() - 1) == '{' && s.charAt(i) == '}')
9 || (stack.get(stack.size() - 1) == '(' && s.charAt(i) == ')')
10 || (stack.get(stack.size() - 1) == '[' && s.charAt(i) == ']'))) {
11 stack.remove(stack.size() - 1);
12 } else {
13 return false;
14 }
15 }
16 return stack.size() > 0 ? false : true;
17 }
18
19 public static void main(String[] args) {
20 System.out.println(valid("()"));
21 System.out.println(valid("()[]{}"));
22 System.out.println(valid("(]"));
23
24 }
25}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected