Created
April 22, 2018 19:07
-
-
Save slavik112211/2edd795136614fa77fc324f0a8c33597 to your computer and use it in GitHub Desktop.
Parenthesis.java
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
// 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