Skip to content

Instantly share code, notes, and snippets.

@IngeFrodo
Last active April 17, 2020 16:16
Show Gist options
  • Save IngeFrodo/0fb4f61ca4a940c82c71a21445b23896 to your computer and use it in GitHub Desktop.
Save IngeFrodo/0fb4f61ca4a940c82c71a21445b23896 to your computer and use it in GitHub Desktop.
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