mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
cs: add «678. Valid Parenthesis String»
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
aa6d36233b
commit
1812c2121d
1 changed files with 26 additions and 0 deletions
26
cs/valid-parenthesis-string.cs
Normal file
26
cs/valid-parenthesis-string.cs
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
public class Solution {
|
||||||
|
public bool CheckValidString(string s) {
|
||||||
|
int left = 0;
|
||||||
|
int right = 0;
|
||||||
|
|
||||||
|
for (int i = 0, j = s.Length - 1; i < s.Length; ++i, --j) {
|
||||||
|
if (s[i] == '(' || s[i] == '*') {
|
||||||
|
++left;
|
||||||
|
} else {
|
||||||
|
--left;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (s[j] == ')' || s[j] == '*') {
|
||||||
|
++right;
|
||||||
|
} else {
|
||||||
|
--right;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (left < 0 || right < 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue