Skip to content

Commit 7576743

Browse files
authored
Merge pull request #1207 from ivan1016017/march11
adding valid parentheses
2 parents a9314c1 + 98c33f9 commit 7576743

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
else:
14+
if len(check_lst) == 0\
15+
or dic_parentheses[check_lst.pop()] != p:
16+
return False
17+
18+
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_14\
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)