Skip to content

Commit 7ca24c5

Browse files
authored
Merge pull request #1137 from ivan1016017/december30
adding valid parentheses
2 parents fb8108f + bb41a40 commit 7ca24c5

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def isValid(self, s):
6+
7+
dic_parentheses = {'(':')','[':']','{':'}'}
8+
check_lst = list()
9+
10+
for p in s:
11+
if p in dic_parentheses:
12+
check_lst.append(p)
13+
elif len(check_lst) == 0 or p != dic_parentheses[check_lst.pop()]:
14+
return False
15+
16+
return len(check_lst) == 0
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_12\
3+
.valid_parentheses import Solution
4+
5+
class ValidParenthesesTestCase(unittest.TestCase):
6+
7+
def test_is_valid_parentheses(self):
8+
solution = Solution()
9+
output = solution.isValid(s='()')
10+
self.assertTrue(output)
11+
12+
def test_is_no_valid_parentheses_pattern_1(self):
13+
solution = Solution()
14+
output = solution.isValid(s='(]')
15+
self.assertFalse(output)
16+
17+
def test_is_no_valid_parentheses_pattern_2(self):
18+
solution = Solution()
19+
output = solution.isValid(s='((')
20+
self.assertFalse(output)

0 commit comments

Comments
 (0)