forked from jamil-said/code-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbraces.py
More file actions
executable file
·54 lines (38 loc) · 1.16 KB
/
braces.py
File metadata and controls
executable file
·54 lines (38 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
""" braces
Given an array of strings containing three types of braces: round (),
square [] and curly {}
Your task is to
write a function that checks whether the braces in each string are
correctly matched
prints 1 to standard output (stdout) if the braces in each string are
matched and 0 if they're not (one result per line)
Note that your function will receive the following arguments:
expressions
which is an array of strings containing braces
Data constraints
the length of the array will not exceed 100
the length of any string will not exceed 5000
Efficiency constraints
your function is expected to print the result in less than 2 seconds
Example Input Output
expressions: [ ")(){}", "", "([])", "{()[]}", "([)]" ]
0 1 1 1 0
"""
def isMatched(arr):
for st in arr: print(calcMatched(st))
def calcMatched(s):
stack, setOp, dicCl = [], {'(','{','['}, {')':'(', '}':'{', ']':'['}
for c in s:
if c in setOp:
stack.append(c)
elif c in dicCl:
if len(stack) == 0 or stack.pop() != dicCl[c]: return 0
return 1 if len(stack) == 0 else 0
isMatched([")(){}", "[]({})", "([])", "{()[]}", "([)]"])
"""
0
1
1
1
0
"""