Skip to content

Commit fd55220

Browse files
authored
Merge pull request #1518 from ivan1016017/january16
January16
2 parents f640d1e + e94bd5c commit fd55220

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
9+
check_list = list()
10+
11+
for p in s:
12+
13+
if p in dic_parentheses:
14+
check_list.append(p)
15+
else:
16+
if len(check_list) == 0 or \
17+
dic_parentheses[check_list.pop()] != p:
18+
19+
return False
20+
21+
return len(check_list) == 0
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function isValid(s: string): boolean {
2+
3+
const dicParentheses: {[key: string]: string} = {
4+
'(': ')',
5+
'[': ']',
6+
'{': '}'
7+
}
8+
9+
const checkList: string[] = []
10+
11+
for (const p of s){
12+
if (p in dicParentheses){
13+
checkList.push(p)
14+
}else{
15+
if (checkList.length === 0 ||
16+
dicParentheses[checkList.pop()!] !== p){
17+
return false
18+
}
19+
}
20+
}
21+
22+
return checkList.length === 0
23+
24+
};
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_22\
3+
.ex_50_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)