Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit49d15b1

Browse files
1021 Remove Outermost Parentheses.py
1 parent79b31de commit49d15b1

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/usr/bin/python3
2+
"""
3+
A valid parentheses string is either empty (""), "(" + A + ")", or A + B, where
4+
A and B are valid parentheses strings, and + represents string concatenation.
5+
For example, "", "()", "(())()", and "(()(()))" are all valid parentheses strings.
6+
7+
A valid parentheses string S is primitive if it is nonempty, and there does not
8+
exist a way to split it into S = A+B, with A and B nonempty valid parentheses
9+
strings.
10+
11+
Given a valid parentheses string S, consider its primitive decomposition:
12+
S = P_1 + P_2 + ... + P_k, where P_i are primitive valid parentheses strings.
13+
14+
Return S after removing the outermost parentheses of every primitive string in
15+
the primitive decomposition of S.
16+
17+
Example 1:
18+
Input: "(()())(())"
19+
Output: "()()()"
20+
Explanation:
21+
The input string is "(()())(())", with primitive decomposition "(()())" + "(())".
22+
After removing outer parentheses of each part, this is "()()" + "()" = "()()()".
23+
24+
Example 2:
25+
Input: "(()())(())(()(()))"
26+
Output: "()()()()(())"
27+
Explanation:
28+
The input string is "(()())(())(()(()))", with primitive decomposition
29+
"(()())" + "(())" + "(()(()))".
30+
After removing outer parentheses of each part, this is "()()" + "()" +
31+
"()(())" = "()()()()(())".
32+
33+
Example 3:
34+
Input: "()()"
35+
Output: ""
36+
Explanation:
37+
The input string is "()()", with primitive decomposition "()" + "()".
38+
After removing outer parentheses of each part, this is "" + "" = "".
39+
40+
41+
Note:
42+
S.length <= 10000
43+
S[i] is "(" or ")"
44+
S is a valid parentheses string
45+
"""
46+
fromcollectionsimportdeque
47+
48+
49+
classSolution:
50+
defremoveOuterParentheses(self,S:str)->str:
51+
"""
52+
Primitive parentheses will have equal number of opened and closed
53+
parentheses.
54+
55+
Use count
56+
Exclude the first and last parathesis
57+
"""
58+
ret= []
59+
cnt=0
60+
foreinS:
61+
ife=="(":
62+
cnt+=1
63+
ifcnt>1:
64+
ret.append(e)
65+
else:
66+
cnt-=1
67+
ifcnt>0:
68+
ret.append(e)
69+
70+
return"".join(ret)
71+
72+
73+
defremoveOuterParentheses_error(self,S:str)->str:
74+
"""
75+
stack + deque
76+
"""
77+
ret= []
78+
stk= []
79+
cur_q=deque()
80+
foreinS:
81+
ife=="(":
82+
stk.append(e)
83+
else:
84+
prev=stk.pop()
85+
ifstk:
86+
cur_q.appendleft(prev)
87+
cur_q.append(e)
88+
else:
89+
ret.extend(cur_q)
90+
cur_q=deque()
91+
92+
return"".join(ret)
93+
94+
95+
if__name__=="__main__":
96+
assertSolution().removeOuterParentheses("(()())(())(()(()))")=="()()()()(())"

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp