Skip to content

Commit d9f9e2b

Browse files
committed
Solve: valid-parentheses
1 parent 37eb1ad commit d9f9e2b

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

valid-parentheses/Jay-Mo-99.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
 #해석
2+
#매개변수 string s의 각 character인 c 가 open bracket이면 temp 리스트에 추가한다.
3+
#c가 close bracket이면 temp의 마지막 element와 짝이 맞는지 검사한다. 짝이 아니거나 temp에 아무 요소도 없으면 return false
4+
#검사 이후 temp에 잔여 요소가 남아있으면 짝이 맞지 않았다는 뜻이니 return false, 아닐 경우 return true
5+
6+
#Big O
7+
#- N: 문자열 s의 길이
8+
9+
#Time Complexity: O(N) = O(N) + O(1)
10+
#- for c in s : string s의 character의 수 만큼 진행된다. -> O(N)
11+
#-temp.append(c), temp.pop() : 리스트 연산은 상수 취급 -> O(1)
12+
13+
#Space Complexity: O(N)
14+
#- temp : list temp은 최대 string s의 character수 만큼 요소를 저장할 가능성이 있다.
15+
16+
17+
class Solution(object):
18+
def isValid(self, s):
19+
"""
20+
:type s: str
21+
:rtype: bool
22+
"""
23+
temp = []
24+
for c in s:
25+
#If c is Open bracket, append to the list
26+
if (c == "(") or (c=="{") or (c=="["):
27+
temp.append(c)
28+
#If C is Close bracket, Check the close bracket pairs with last elememt of temp list
29+
else:
30+
#There's no element in the tmep, Return false
31+
if(len(temp)==0):
32+
return False
33+
34+
if(c==")") and (temp.pop()=="("):
35+
continue
36+
if(c=="}") and (temp.pop()=="{"):
37+
continue
38+
if(c=="]") and (temp.pop()=="["):
39+
continue
40+
else:
41+
return False
42+
43+
#After loop, Check temp is empty or not.
44+
#If all c of s is pairs each other, the temp list is empty.
45+
if (len(temp) == 0) :
46+
return True
47+
else:
48+
return False
49+
50+
51+

0 commit comments

Comments
 (0)