Skip to content

Commit 3bed800

Browse files
committed
Solution for 1021-1023
1 parent 79ec58b commit 3bed800

File tree

4 files changed

+192
-0
lines changed

4 files changed

+192
-0
lines changed

1000-1100q/1021.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'''
2+
A valid parentheses string is either empty (""), "(" + A + ")", or A + B, where A and B are valid parentheses strings, and + represents string concatenation. For example, "", "()", "(())()", and "(()(()))" are all valid parentheses strings.
3+
4+
A valid parentheses string S is primitive if it is nonempty, and there does not exist a way to split it into S = A+B, with A and B nonempty valid parentheses strings.
5+
6+
Given a valid parentheses string S, consider its primitive decomposition: S = P_1 + P_2 + ... + P_k, where P_i are primitive valid parentheses strings.
7+
8+
Return S after removing the outermost parentheses of every primitive string in the primitive decomposition of S.
9+
10+
11+
12+
Example 1:
13+
14+
Input: "(()())(())"
15+
Output: "()()()"
16+
Explanation:
17+
The input string is "(()())(())", with primitive decomposition "(()())" + "(())".
18+
After removing outer parentheses of each part, this is "()()" + "()" = "()()()".
19+
Example 2:
20+
21+
Input: "(()())(())(()(()))"
22+
Output: "()()()()(())"
23+
Explanation:
24+
The input string is "(()())(())(()(()))", with primitive decomposition "(()())" + "(())" + "(()(()))".
25+
After removing outer parentheses of each part, this is "()()" + "()" + "()(())" = "()()()()(())".
26+
Example 3:
27+
28+
Input: "()()"
29+
Output: ""
30+
Explanation:
31+
The input string is "()()", with primitive decomposition "()" + "()".
32+
After removing outer parentheses of each part, this is "" + "" = "".
33+
34+
35+
Note:
36+
37+
S.length <= 10000
38+
S[i] is "(" or ")"
39+
S is a valid parentheses string
40+
'''
41+
42+
class Solution(object):
43+
def removeOuterParentheses(self, S):
44+
"""
45+
:type S: str
46+
:rtype: str
47+
"""
48+
temp, result = "", ""
49+
start_bracket = 0
50+
for char in S:
51+
temp += char
52+
if char == '(':
53+
start_bracket += 1
54+
else:
55+
start_bracket -= 1
56+
if start_bracket == 0:
57+
result += temp[1:-1]
58+
temp = ""
59+
return result

1000-1100q/1022.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'''
2+
Given a binary tree, each node has value 0 or 1. Each root-to-leaf path represents a binary number starting with the most significant bit. For example, if the path is 0 -> 1 -> 1 -> 0 -> 1, then this could represent 01101 in binary, which is 13.
3+
4+
For all leaves in the tree, consider the numbers represented by the path from the root to that leaf.
5+
6+
Return the sum of these numbers.
7+
8+
9+
10+
Example 1:
11+
12+
13+
14+
Input: [1,0,1,0,1,0,1]
15+
Output: 22
16+
Explanation: (100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22
17+
18+
19+
Note:
20+
21+
The number of nodes in the tree is between 1 and 1000.
22+
node.val is 0 or 1.
23+
The answer will not exceed 2^31 - 1.
24+
'''
25+
# Definition for a binary tree node.
26+
# class TreeNode(object):
27+
# def __init__(self, x):
28+
# self.val = x
29+
# self.left = None
30+
# self.right = None
31+
32+
class Solution(object):
33+
def sumRootToLeaf(self, root):
34+
"""
35+
:type root: TreeNode
36+
:rtype: int
37+
"""
38+
def traversal(root, paths, pathlen, allpaths):
39+
if not root:
40+
return
41+
if len(paths) > pathlen:
42+
paths[pathlen] = root.val
43+
else:
44+
paths.append(root.val)
45+
46+
pathlen +=1
47+
if not root.left and not root.right:
48+
allpaths.append(int(''.join(str(val) for val in paths[0:pathlen]), 2))
49+
else:
50+
traversal(root.left, paths, pathlen, allpaths)
51+
traversal(root.right, paths, pathlen, allpaths)
52+
paths = []
53+
traversal(root, [], 0, paths)
54+
return sum(paths)%1000000007

1000-1100q/1023.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
'''
2+
A query word matches a given pattern if we can insert lowercase letters to the pattern word so that it equals the query. (We may insert each character at any position, and may insert 0 characters.)
3+
4+
Given a list of queries, and a pattern, return an answer list of booleans, where answer[i] is true if and only if queries[i] matches the pattern.
5+
6+
7+
8+
Example 1:
9+
10+
Input: queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FB"
11+
Output: [true,false,true,true,false]
12+
Explanation:
13+
"FooBar" can be generated like this "F" + "oo" + "B" + "ar".
14+
"FootBall" can be generated like this "F" + "oot" + "B" + "all".
15+
"FrameBuffer" can be generated like this "F" + "rame" + "B" + "uffer".
16+
Example 2:
17+
18+
Input: queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBa"
19+
Output: [true,false,true,false,false]
20+
Explanation:
21+
"FooBar" can be generated like this "Fo" + "o" + "Ba" + "r".
22+
"FootBall" can be generated like this "Fo" + "ot" + "Ba" + "ll".
23+
Example 3:
24+
25+
Input: queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBaT"
26+
Output: [false,true,false,false,false]
27+
Explanation:
28+
"FooBarTest" can be generated like this "Fo" + "o" + "Ba" + "r" + "T" + "est".
29+
30+
31+
Note:
32+
33+
1. 1 <= queries.length <= 100
34+
2. 1 <= queries[i].length <= 100
35+
3. 1 <= pattern.length <= 100
36+
4. All strings consists only of lower and upper case English letters.
37+
'''
38+
39+
class Solution(object):
40+
def camelMatch(self, queries, pattern):
41+
"""
42+
:type queries: List[str]
43+
:type pattern: str
44+
:rtype: List[bool]
45+
"""
46+
import re
47+
result = []
48+
patterns = re.findall('[A-Z][a-z]*', pattern)
49+
50+
for query in queries:
51+
splitter = re.findall('[A-Z][a-z]*', query)
52+
flag = True
53+
if len(patterns) == len(splitter):
54+
for index in range(len(patterns)):
55+
# print patterns[index], splitter[index]
56+
p_i, s_i = 1, 1
57+
if patterns[index][0] == splitter[index][0]:
58+
while p_i < len(patterns[index]) and s_i < len(splitter[index]):
59+
if patterns[index][p_i] == splitter[index][s_i]:
60+
p_i += 1
61+
s_i += 1
62+
else:
63+
s_i += 1
64+
if p_i != len(patterns[index]):
65+
flag = False
66+
break
67+
else:
68+
flag = False
69+
break
70+
if flag:
71+
result.append(True)
72+
else:
73+
result.append(False)
74+
else:
75+
result.append(False)
76+
return result

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
77
##### [Problems 1000-1100](./1000-1100q/)
88
| # | Title | Solution | Difficulty |
99
|---| ----- | -------- | ---------- |
10+
|1023|[Camelcase Matching](https://leetcode.com/problems/camelcase-matching)|[Python](./1000-1100q/1023.py)|Medium|
11+
|1022|[Sum of Root To Leaf Binary Numbers](https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers)|[Python](./1000-1100q/1022.py)|Easy|
12+
|1021|[Remove Outermost Parentheses](https://leetcode.com/problems/remove-outermost-parentheses)|[Python](./1000-1100q/1021.py)|Easy|
1013
|1019|[Next Greater Node In Linked List](https://leetcode.com/problems/next-greater-node-in-linked-list)|[Python](./1000-1100q/1019.py)|Medium|
1114
|1018|[Binary Prefix Divisible By 5](https://leetcode.com/problems/binary-prefix-divisible-by-5)|[Python](./1000-1100q/1018.py)|Easy|
1215
|1017|[Convert to Base -2](https://leetcode.com/problems/convert-to-base-2)|[Python](./1000-1100q/1017.py)|Medium|

0 commit comments

Comments
 (0)