Last active
April 17, 2020 16:16
-
-
Save IngeFrodo/0fb4f61ca4a940c82c71a21445b23896 to your computer and use it in GitHub Desktop.
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 ValidParenthesisString { | |
public boolean checkValidString(String s) { | |
int openParenthesis = 0; | |
int closedParenthesis = 0; | |
for (int i = 0; i < s.length(); i++) { | |
char c = s.charAt(i); | |
if (c == '(' || c == '*') { | |
openParenthesis++; | |
} else { | |
openParenthesis--; | |
} | |
char e = s.charAt(s.length() - i - 1); | |
if (e == ')' || e == '*') { | |
closedParenthesis++; | |
} else { | |
closedParenthesis--; | |
} | |
if (openParenthesis < 0 || closedParenthesis < 0) { | |
return false; | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment