Skip to content

Commit c6a1319

Browse files
committed
Add problem 678 - Valid Parenthesis String
1 parent 5f85552 commit c6a1319

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class ValidParenthesisString:
2+
@staticmethod
3+
def checkValidString(s: str) -> bool:
4+
# Special case
5+
if s is None or not len(s):
6+
return True
7+
# Count of minimum and maximum open parentheses
8+
min_open, max_open = 0, 0
9+
# Traverse through the string
10+
for c in s:
11+
# If it is a left parenthesis, it means you have one
12+
# more open parenthesis to worry about
13+
if c == '(':
14+
min_open += 1
15+
max_open += 1
16+
# If it is right parenthesis, it means you have one
17+
# less open parenthesis to worry about
18+
elif c == ')':
19+
min_open -= 1
20+
max_open -= 1
21+
# If it is *, it means there are three cases
22+
# 1. If we make it as ), then we have one less open parenthesis to worry about
23+
# 2. If we make it as (, then we have one more open parentheses
24+
# to worry about
25+
# 3. No impact at all
26+
elif c == '*':
27+
min_open -= 1
28+
max_open += 1
29+
30+
# There are more closing parentheses, then opening ones
31+
if max_open < 0:
32+
return False
33+
# We can't have negative open parenthesis count
34+
if min_open < 0:
35+
min_open = 0
36+
37+
return min_open == 0
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import unittest
2+
3+
from problems.greedy.valid_parenthesis_string import ValidParenthesisString
4+
5+
6+
class TestValidParenthesisString(unittest.TestCase):
7+
8+
def setUp(self):
9+
self.valid_parenthesis_string = ValidParenthesisString()
10+
11+
def test_check_valid_string(self):
12+
# Test null input
13+
self.assertTrue(self.valid_parenthesis_string.checkValidString(None))
14+
15+
# Test empty string
16+
self.assertTrue(self.valid_parenthesis_string.checkValidString(""))
17+
18+
# Test valid strings
19+
self.assertTrue(self.valid_parenthesis_string.checkValidString("()"))
20+
self.assertTrue(self.valid_parenthesis_string.checkValidString("(*)"))
21+
self.assertTrue(self.valid_parenthesis_string.checkValidString("(*))"))
22+
23+
# Test invalid strings
24+
self.assertFalse(self.valid_parenthesis_string.checkValidString(")"))
25+
self.assertFalse(self.valid_parenthesis_string.checkValidString(")("))
26+
self.assertTrue(self.valid_parenthesis_string.checkValidString("((*)"))
27+
self.assertFalse(self.valid_parenthesis_string.checkValidString("((*))("))
28+
29+
def test_edge_cases(self):
30+
# Test string with only one *
31+
self.assertTrue(self.valid_parenthesis_string.checkValidString("*"))
32+
33+
# Test string with multiple *
34+
self.assertTrue(self.valid_parenthesis_string.checkValidString("***"))
35+
36+
# Test strings with nested parentheses
37+
self.assertTrue(self.valid_parenthesis_string.checkValidString("(())"))
38+
self.assertFalse(self.valid_parenthesis_string.checkValidString("(()))"))
39+
40+
41+
if __name__ == '__main__':
42+
unittest.main()

0 commit comments

Comments
 (0)