Skip to content

Commit 1fc7e1f

Browse files
committed
Minor formatting fixes
1 parent 9d32375 commit 1fc7e1f

File tree

3 files changed

+34
-29
lines changed

3 files changed

+34
-29
lines changed

sliding_window/minimum_window_substring.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
'''
1+
"""
22
Question: https://leetcode.com/problems/minimum-window-substring/
3-
'''
3+
"""
44

55
class Solution:
66
def minWindow(self, s: str, t: str) -> str:
7-
'''
8-
Sliding window with two pointers -
7+
"""
8+
Approach: Sliding window with two pointers -
99
1010
1. Count the frequency of characters in `t`
1111
2. Maintain two counters - `have` & `need`
@@ -18,8 +18,8 @@ def minWindow(self, s: str, t: str) -> str:
1818
1919
So, we move `right` pointer in `have != need` (the for loop at line 43) and `left` pointer if `have == need` (line 56)
2020
21-
Time complexity: O(n)
22-
'''
21+
Time complexity: O(N)
22+
"""
2323

2424
# Edge case
2525
if not t:

stack/valid_parentheses.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
'''
1+
"""
22
Question: https://leetcode.com/problems/valid-parentheses/
3-
'''
3+
"""
4+
45

56
class Solution:
67
def isValid(self, s: str) -> bool:
@@ -14,9 +15,10 @@ def isValid(self, s: str) -> bool:
1415
# Iterate through the input string
1516
for b in s:
1617
"""
17-
If stack isn't empty AND we have a valid bracket AND if last brack
18-
from stack is equal to closing brack's key from dict i.e. opening,
19-
then remove that pair of brackets from the stack
18+
If `stack` isn't empty AND we have a valid bracket
19+
AND if last brack from stack is equal to closing brack's key
20+
from dict i.e. opening,
21+
then remove that pair of brackets from the stack.
2022
"""
2123

2224
# We check if it's a valid bracket and if the stack is non-empty
@@ -35,17 +37,17 @@ def isValid(self, s: str) -> bool:
3537
else:
3638
stack.append(b)
3739

38-
''' Returns True if stack is empty (checked using 'not stack') and vice-versa'''
40+
""" Returns True if stack is empty (checked using 'not stack') and vice-versa """
3941
return True if not stack else False
4042

41-
''' Alternative code for same approach '''
43+
""" Alternative code for same approach """
4244

4345
# closeToOpen = {")": "(", "]": "[", "}": "{"}
4446
# stack = []
4547

4648
# for c in s:
4749

48-
# # If c not in closeToOpen dict, that means it's an opening bracket
50+
# # If c not in `closeToOpen` dict, that means it's an opening bracket
4951
# if c not in closeToOpen:
5052
# # Add to stack and skip to next iteration
5153
# stack.append(c)
@@ -64,5 +66,3 @@ def isValid(self, s: str) -> bool:
6466

6567
# # `not stack` means stack is empty i.e. valid else False if stack is not empty
6668
# return True if not stack else False
67-
68-

trees/invert_binary_tree.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,36 @@
1-
'''
1+
"""
22
Question: https://leetcode.com/problems/invert-binary-tree/
3-
'''
3+
"""
4+
5+
from typing import Optional
6+
7+
class TreeNode:
8+
"""
9+
Definition for a binary tree node.
10+
"""
11+
def __init__(self, val=0, left=None, right=None):
12+
self.val = val
13+
self.left = left
14+
self.right = right
415

5-
# Definition for a binary tree node.
6-
# class TreeNode:
7-
# def __init__(self, val=0, left=None, right=None):
8-
# self.val = val
9-
# self.left = left
10-
# self.right = right
1116

1217
class Solution:
1318
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
14-
'''
15-
Use Recursion to swap the children of the root. Done using DFS.
16-
'''
19+
"""
20+
Approach: Use Recursive DFS to swap the children of the root.
21+
"""
1722

1823
# Base case for recursion
1924
if not root:
2025
return None
2126

22-
# Swap the Children
27+
# Swap the Children of the current `root`
2328
root.left, root.right = root.right, root.left
2429

2530
# Run invertion on left and right subtrees
2631
self.invertTree(root.left)
2732
self.invertTree(root.right)
2833

29-
# Return the inverted root
34+
# At the end of all recursions, the final `root` will be the inverted tree.
3035
return root
3136

0 commit comments

Comments
 (0)