Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save slavik112211/2edd795136614fa77fc324f0a8c33597 to your computer and use it in GitHub Desktop.
Save slavik112211/2edd795136614fa77fc324f0a8c33597 to your computer and use it in GitHub Desktop.
Parenthesis.java
// https://leetcode.com/problems/valid-parentheses/description/
class Parentheses {
public boolean isValid(String s) {
boolean isValid = true;
if (s.length()%2 != 0) {
isValid = false;
return isValid;
}
for (int i = 0; i < s.length()-1; i=i+2) {
if (s.charAt(i) == ')' || s.charAt(i) == ']' || s.charAt(i) == '}'){
isValid = false;
break;
}
if (s.charAt(i) == '(' && s.charAt(i+1) != ')') {
isValid = false;
break;
}
if (s.charAt(i) == '[' && s.charAt(i+1) != ']') {
isValid = false;
break;
}
if (s.charAt(i) == '{' && s.charAt(i+1) != '}') {
isValid = false;
break;
}
}
return isValid;
}
public static void main(String[] args) {
Parentheses parentheses= new Parentheses();
System.out.println(parentheses.isValid("{}[]())))))("));
}
}
// ({})
// ([])
// {(})
//g containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
//The brackets must close in the correct order, "()" and "()[]{}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment