Skip to content

Commit

Permalink
Solution of Remove Invalid Parentheses and Binary Tree Paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Garvit244 committed Jun 17, 2018
1 parent 4949001 commit 399230d
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 1 deletion.
50 changes: 50 additions & 0 deletions 200-300q/257.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'''
Given a binary tree, return all root-to-leaf paths.
Note: A leaf is a node with no children.
Example:
Input:
1
/ \
2 3
\
5
Output: ["1->2->5", "1->3"]
Explanation: All root-to-leaf paths are: 1->2->5, 1->3
'''

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution(object):
def binaryTreePaths(self, root):
"""
:type root: TreeNode
:rtype: List[str]
"""
if not root:
return []

paths = []
def dfs(root, curr):
if root.left is None and root.right is None:
paths.append(curr + str(root.val))
return

if root.left:
dfs(root.left, curr + str(root.val) + '->')
if root.right:
dfs(root.right, curr + str(root.val) + '->')

curr = ""
dfs(root, curr)
return paths
63 changes: 63 additions & 0 deletions 300-400q/301.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'''
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results.
Note: The input string may contain letters other than the parentheses ( and ).
Example 1:
Input: "()())()"
Output: ["()()()", "(())()"]
Example 2:
Input: "(a)())()"
Output: ["(a)()()", "(a())()"]
Example 3:
Input: ")("
Output: [""]
'''

class Solution(object):
def removeInvalidParentheses(self, s):
"""
:type s: str
:rtype: List[str]
"""
if not s:
return [""]

def isValid(s):
count = 0
for char in s:
if char == '(':
count += 1
elif char == ')':
count -= 1
if count < 0:
return False
return (count==0)

queue, result = [s], []
visited = set()
visited.add(s)
level = False

while queue:
new_str = queue.pop(0)
if isValid(new_str):
result.append(new_str)
level = True

if level:
continue

for index in range(len(new_str)):
if not (new_str[index] == "(" or new_str[index] == ")"):
continue
partition_str = new_str[0:index] + new_str[index+1:]
if partition_str not in visited:
queue.append(partition_str)
visited.add(partition_str)
return result


3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
|326|[Power of Three](https://leetcode.com/problems/power-of-three/) | [Python](./300-400q/326.py)|Easy|
|322|[Coin Change](https://leetcode.com/problems/coin-change/) | [Python](./300-400q/322.py)|Medium|
|315|[Count of Smaller Numbers After Self](https://leetcode.com/problems/count-of-smaller-numbers-after-self/) | [Python](./300-400q/315.py)|Hard|

|301|[Remove Invalid Parentheses](https://leetcode.com/problems/remove-invalid-parentheses)|[Python](./300-400q/301.py)|Hard|

##### [Problems 200-300q](./200-300q/)
| # | Title | Solution | Difficulty |
Expand All @@ -32,6 +32,7 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
|283|[Move Zeros](https://leetcode.com/problems/move-zeroes)|[Python](./200-300q/283.py)|Easy|
|279|[Perfect Squares](https://leetcode.com/problems/perfect-squares/) | [Python](./200-300q/279.py)|Medium|
|268|[Missing Number](https://leetcode.com/problems/missing-number)|[Python](./200-300q/268.py)|Easy|
|257|[Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths)[Python](./200-300q/257.py)|Easy|
|253|[Meeting Rooms II](https://leetcode.com/problems/meeting-rooms-ii) | [Python](./200-300q/253.py)|Medium|
|240|[Search a 2D Matrix II](https://leetcode.com/problems/search-a-2d-matrix-ii/)|[Python](./200-300q/240.py)|Medium|
|239|[Sliding Window Maximum](https://leetcode.com/problems/sliding-window-maximum/)| [Python](./200-300q/239.py)|Hard|
Expand Down

0 comments on commit 399230d

Please sign in to comment.