MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / isValid

Method isValid

ValidParenthesis.java:28–46  ·  view source on GitHub ↗
(String s)

Source from the content-addressed store, hash-verified

26 }
27
28 public boolean isValid(String s) {
29
30 Stack<Character> stack = new Stack<>();
31 for(int i=0;i<s.length();i++)
32 {
33 if(isOpen(s.charAt(i))) stack.push(s.charAt(i));
34 else
35 {
36 if(stack.isEmpty()) return false;
37
38 if(isOpen(stack.peek()) && isSame(s.charAt(i),stack.peek()))
39 {
40 stack.pop();
41 }
42 else return false;
43 }
44 }
45 return (stack.isEmpty());
46 }
47}

Callers

nothing calls this directly

Calls 5

isOpenMethod · 0.95
isSameMethod · 0.95
pushMethod · 0.80
popMethod · 0.80
isEmptyMethod · 0.45

Tested by

no test coverage detected