Created
August 16, 2022 19:44
-
-
Save saayv1/770a07e23df2fa188f773a34d1d31b9c to your computer and use it in GitHub Desktop.
cassidoo-interview-question-august-14
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int longestValidParentheses(String s) { | |
ArrayDeque<Integer> stack = new ArrayDeque<>(); | |
int max = 0; | |
stack.push(-1); | |
for(int i=0;i<s.length();i++) { | |
if(s.charAt(i) == '(') { | |
stack.push(i); | |
} else { | |
stack.pop(); | |
if(stack.isEmpty() ) { | |
stack.push(i); | |
} else { | |
max = Math.max(max, i-stack.peek()); | |
} | |
} | |
} | |
return max; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment