Skip to content

Commit

Permalink
Adding 678-Valid-Parenthesis-String.c
Browse files Browse the repository at this point in the history
  • Loading branch information
FahadulShadhin committed Aug 24, 2022
1 parent 1972d20 commit 662dca4
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions c/678-Valid-Parenthesis-String.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Time: O(n)
Space: O(1)
*/

bool checkValidString(char * s) {
int n = strlen(s);

int balanced = 0;
for (int i=0; i<n; i++) {
if (s[i] == '(' || s[i] == '*')
balanced++;
else
balanced--;

if (balanced < 0)
return false;
}

if (balanced == 0)
return true;

balanced = 0;
for (int i=n-1; i>=0; i--) {
if (s[i] == ')' || s[i] == '*')
balanced++;
else
balanced--;

if (balanced < 0)
return false;
}

return true;
}

0 comments on commit 662dca4

Please sign in to comment.