Skip to content

Instantly share code, notes, and snippets.

@saayv1
Created August 16, 2022 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saayv1/770a07e23df2fa188f773a34d1d31b9c to your computer and use it in GitHub Desktop.
Save saayv1/770a07e23df2fa188f773a34d1d31b9c to your computer and use it in GitHub Desktop.
cassidoo-interview-question-august-14
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