From 33ae545dd89b774ac46a2ffa32935126ce6e7137 Mon Sep 17 00:00:00 2001 From: Moein Arabi Date: Tue, 13 Aug 2024 13:52:10 +0000 Subject: [PATCH] Create 0678-valid-parenthesis-string.go --- go/0678-valid-parenthesis-string.go | 39 +++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 go/0678-valid-parenthesis-string.go diff --git a/go/0678-valid-parenthesis-string.go b/go/0678-valid-parenthesis-string.go new file mode 100644 index 000000000..60f2e8543 --- /dev/null +++ b/go/0678-valid-parenthesis-string.go @@ -0,0 +1,39 @@ +func checkValidString(s string) bool { + stars := 0 + open := 0 + for _, v := range s { + if v == '(' { + open++ + } else if v == ')' { + if open > 0 { + open-- + } else if stars > 0 { + stars-- + } else { + return false + } + } else { + stars++ + } + } + + close := 0 + stars = 0 + for i := len(s) - 1; i >= 0; i-- { + v := s[i] + if v == ')' { + close++ + } else if v == '(' { + if close > 0 { + close-- + } else if stars > 0 { + stars-- + } else { + return false + } + } else { + stars++ + } + } + return true +} \ No newline at end of file