From 1812c2121d8bea7a6431f8701a9b56e6138bb4e5 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sun, 7 Apr 2024 20:57:39 +0200 Subject: [PATCH] =?UTF-8?q?cs:=20add=20=C2=AB678.=20Valid=20Parenthesis=20?= =?UTF-8?q?String=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- cs/valid-parenthesis-string.cs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 cs/valid-parenthesis-string.cs diff --git a/cs/valid-parenthesis-string.cs b/cs/valid-parenthesis-string.cs new file mode 100644 index 0000000..172033e --- /dev/null +++ b/cs/valid-parenthesis-string.cs @@ -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; + } +}