Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions clone-graph/imgolden77.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
# Definition for a Node.
class Node:
def __init__(self, val = 0, neighbors = None):
self.val = val
self.neighbors = neighbors if neighbors is not None else []
"""

from typing import Optional
class Solution:
def cloneGraph(self, node: Optional['Node']) -> Optional['Node']:
if not node:
return None
old_to_new = {}

def dfs(curr_node):
if curr_node in old_to_new:
return old_to_new[curr_node]

copy = Node(curr_node.val)
old_to_new[curr_node] = copy

for neighbors in curr_node.neighbors:
copy.neighbors.append(dfs(neighbors))
Comment on lines +23 to +24
Copy link

Copilot AI Jan 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable name 'neighbors' in the loop is misleading because it represents a single neighbor node, not multiple neighbors. It should be singular, like 'neighbor', to accurately reflect what it represents.

Suggested change
for neighbors in curr_node.neighbors:
copy.neighbors.append(dfs(neighbors))
for neighbor in curr_node.neighbors:
copy.neighbors.append(dfs(neighbor))

Copilot uses AI. Check for mistakes.

return copy

return dfs(node)

# O(N+E) time complexity where N is the number of nodes and E is the number of edges in the graph.
# O(N) space complexity for the hashmap and the recursion stack in the worst case.


27 changes: 27 additions & 0 deletions longest-repeating-character-replacement/imgolden77.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution:
def characterReplacement(self, s: str, k: int) -> int:
start = 0
max_len = 0
seen = {}

for i, char in enumerate(s):

seen[char] = seen.get(char, 0) + 1
# what I wrote initially:
# if char not in seen:
# seen[char] = 1
# else:
# seen[char] += 1
Comment on lines +9 to +14
Copy link

Copilot AI Jan 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment appears to contain commented-out code.

Suggested change
seen[char] = seen.get(char, 0) + 1
# what I wrote initially:
# if char not in seen:
# seen[char] = 1
# else:
# seen[char] += 1
seen[char] = seen.get(char, 0) + 1 # update frequency of current character

Copilot uses AI. Check for mistakes.
max_val = max(list(seen.values()))
Copy link

Copilot AI Jan 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating a list from dictionary values with 'list(seen.values())' and then calling 'max()' on each iteration is inefficient. Consider tracking the maximum frequency as you update the dictionary, storing it in a variable like 'max_freq' and updating it when you increment character counts.

Copilot uses AI. Check for mistakes.
window_len = i - start + 1
if window_len - max_val <= k:
max_len = max(max_len, window_len)
else:
seen[s[start]] -= 1
start += 1

return max_len

# O(N) time complexity and O(1) space complexity.


33 changes: 33 additions & 0 deletions longest-substring-without-repeating-characters/imgolden77.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
h = []
max_s = 0
for i in s:
if i not in h:
h.append(i)
else:
h=h[h.index(i)+1:]
h.append(i)
#I reset h=[] but it had problemswith the cases like "dvdf"
#So I modified it to h=h[h.index(i)+1:]
max_s = max(max_s, len(h))

return max_s

#O(N^2) time complexity due to "if i not in h" and O(min(m,n)) space complexity where m is the size of the charset and n is the size of the string s.
Comment on lines +3 to +17
Copy link

Copilot AI Jan 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable name 'h' is not descriptive. Consider using a more meaningful name like 'char_window' or 'current_chars' to indicate that it stores the current substring being evaluated.

Suggested change
h = []
max_s = 0
for i in s:
if i not in h:
h.append(i)
else:
h=h[h.index(i)+1:]
h.append(i)
#I reset h=[] but it had problemswith the cases like "dvdf"
#So I modified it to h=h[h.index(i)+1:]
max_s = max(max_s, len(h))
return max_s
#O(N^2) time complexity due to "if i not in h" and O(min(m,n)) space complexity where m is the size of the charset and n is the size of the string s.
char_window = []
max_s = 0
for i in s:
if i not in char_window:
char_window.append(i)
else:
char_window = char_window[char_window.index(i) + 1:]
char_window.append(i)
#I reset char_window=[] but it had problemswith the cases like "dvdf"
#So I modified it to char_window=char_window[char_window.index(i)+1:]
max_s = max(max_s, len(char_window))
return max_s
#O(N^2) time complexity due to "if i not in char_window" and O(min(m,n)) space complexity where m is the size of the charset and n is the size of the string s.

Copilot uses AI. Check for mistakes.

class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
seen = {}
start = 0
max_len = 0
for i, char in enumerate(s):
if char in seen and seen[char]>=start:
start= seen[char]+1
seen[char]=i
max_len = max(max_len, i - start +1)

Comment on lines +3 to +29
Copy link

Copilot AI Jan 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assignment to 'Solution' is unnecessary as it is redefined before this value is used.

Suggested change
h = []
max_s = 0
for i in s:
if i not in h:
h.append(i)
else:
h=h[h.index(i)+1:]
h.append(i)
#I reset h=[] but it had problemswith the cases like "dvdf"
#So I modified it to h=h[h.index(i)+1:]
max_s = max(max_s, len(h))
return max_s
#O(N^2) time complexity due to "if i not in h" and O(min(m,n)) space complexity where m is the size of the charset and n is the size of the string s.
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
seen = {}
start = 0
max_len = 0
for i, char in enumerate(s):
if char in seen and seen[char]>=start:
start= seen[char]+1
seen[char]=i
max_len = max(max_len, i - start +1)
seen = {}
start = 0
max_len = 0
for i, char in enumerate(s):
if char in seen and seen[char] >= start:
start = seen[char] + 1
seen[char] = i
max_len = max(max_len, i - start + 1)

Copilot uses AI. Check for mistakes.
return max_len
#O(N) time complexity and O(min(m,n)) space complexity where m is the size of the charset and n is the size of the string s.
#Dictionary search and insert operations are O(1) on average. so it is O(N) overall.

Empty file added number-of-islands/imgolden77.py
Empty file.
25 changes: 25 additions & 0 deletions palindromic-substrings/imgolden77.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution:
def countSubstrings(self, s: str) -> int:
total_count = 0

def expand(left, right):
local_count = 0
while left>=0 and right< len(s) and s[left] == s[right]:
# while s[left] == s[right] and left>=0 and right< len(s) :
# The above line would cause index error if left or right go out of bounds
local_count += 1
left -= 1
right += 1
return local_count

for i in range(len(s)):
total_count += expand(i, i)
total_count += expand(i, i+1)

return total_count

#Complexity Analysis
#Time complexity: O(n^2) where n is the length of the input string s.
#Space complexity: O(1) as we are using only a constant amount of extra space
# Manacher's Algorithm can achieve O(n) time complexity for this problem.

16 changes: 16 additions & 0 deletions reverse-bits/imgolden77.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution:
def reverseBits(self, n: int) -> int:
if n == 0:
return 0
res = 0
for i in range(32):
bit = n & 1
res = res + bit
res = res << 1
n = n >> 1
res = res >> 1

return res

# Complexity O(1) - Time: 32 iterations, Space: O(1) no extra space used

21 changes: 21 additions & 0 deletions reverse-linked-list/imgolden77.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
curr = head
prev = None

while curr is not None: # once was "while curr.next is not None:" but that showed AttributeError: 'NoneType' object has no attribute 'next'
if prev:
post = curr.next
curr.next = prev
prev = curr
curr = post
else:
post = curr.next
prev = curr
curr.next = None
curr = post
Comment on lines +7 to +16
Copy link

Copilot AI Jan 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition 'if prev:' is redundant and creates unnecessary code duplication. The logic inside both branches is nearly identical. You can simplify by removing the conditional check since the operations work correctly regardless of whether prev is None or not.

Suggested change
if prev:
post = curr.next
curr.next = prev
prev = curr
curr = post
else:
post = curr.next
prev = curr
curr.next = None
curr = post
post = curr.next
curr.next = prev
prev = curr
curr = post

Copilot uses AI. Check for mistakes.

return prev

# First time, return Curr, but the result was [] so I changed to "return prev" as the new head of the reversed Linked list.

59 changes: 59 additions & 0 deletions valid-parentheses/imgolden77.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# First try O(n) time complexity
class Solution:
def isValid(self, s: str) -> bool:
stack = []
former= ['(', '{', '[']

for i in s:
if i in former:
Comment on lines +5 to +8
Copy link

Copilot AI Jan 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable name 'former' should be 'opening' or 'opening_brackets' to better describe its purpose. The name 'former' is ambiguous and doesn't clearly convey that it contains opening parentheses.

Suggested change
former= ['(', '{', '[']
for i in s:
if i in former:
opening_brackets = ['(', '{', '[']
for i in s:
if i in opening_brackets:

Copilot uses AI. Check for mistakes.
stack.append(i)
elif i == ')':
if stack== []:
Copy link

Copilot AI Jan 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent spacing around the equality operator. Should be 'stack == []' to match Python style conventions (PEP 8).

Copilot uses AI. Check for mistakes.
return False
elif stack[-1]== '(':
Copy link

Copilot AI Jan 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent spacing around the equality operator. Should be 'stack[-1] == ' to match Python style conventions (PEP 8).

Copilot uses AI. Check for mistakes.
stack.pop()
else:
return False
elif i == '}':
if stack== []:
Copy link

Copilot AI Jan 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent spacing around the equality operator. Should be 'stack == []' to match Python style conventions (PEP 8).

Copilot uses AI. Check for mistakes.
return False
elif stack[-1]== '{':
Copy link

Copilot AI Jan 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent spacing around the equality operator. Should be 'stack[-1] == ' to match Python style conventions (PEP 8).

Copilot uses AI. Check for mistakes.
stack.pop()
else:
return False
elif i == ']':
if stack == []:
return False
elif stack[-1]== '[':
Copy link

Copilot AI Jan 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent spacing around the equality operator. Should be 'stack[-1] == ' to match Python style conventions (PEP 8).

Copilot uses AI. Check for mistakes.
stack.pop()
else:
return False


if stack == []:
return True
else:
return False

Comment on lines +2 to +37
Copy link

Copilot AI Jan 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assignment to 'Solution' is unnecessary as it is redefined before this value is used.

Suggested change
class Solution:
def isValid(self, s: str) -> bool:
stack = []
former= ['(', '{', '[']
for i in s:
if i in former:
stack.append(i)
elif i == ')':
if stack== []:
return False
elif stack[-1]== '(':
stack.pop()
else:
return False
elif i == '}':
if stack== []:
return False
elif stack[-1]== '{':
stack.pop()
else:
return False
elif i == ']':
if stack == []:
return False
elif stack[-1]== '[':
stack.pop()
else:
return False
if stack == []:
return True
else:
return False
# (obsolete implementation removed; see the Solution class below)

Copilot uses AI. Check for mistakes.
# Second try: O(n) time complexity
# refer other people's code
# 'not stack' is more pythonic way to check empty stack
# par_map dictionary to map closing to opening parentheses
class Solution:
def isValid(self, s: str) -> bool:
stack = []
par_map = {')':'(', '}':'{', ']':'['}

for i in s:
if i in par_map.values():
stack.append(i)
elif i in par_map:
if not stack or stack[-1] !=par_map[i]:
Copy link

Copilot AI Jan 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent spacing around the inequality operator. Should be 'stack[-1] != par_map[i]' to match Python style conventions (PEP 8).

Suggested change
if not stack or stack[-1] !=par_map[i]:
if not stack or stack[-1] != par_map[i]:

Copilot uses AI. Check for mistakes.
return False
stack.pop()
else:
return False

return not stack