Skip to content
Closed
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))

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.


28 changes: 28 additions & 0 deletions longest-repeating-character-replacement/imgolden77.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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
# increment the count for the current character in the window
seen[char] = seen.get(char, 0) + 1

Copilot uses AI. Check for mistakes.
max_val = max(list(seen.values()))
window_len = i - start + 1
if window_len - max_val <= k:
Comment on lines +6 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.

Creating a new list from dictionary values on every iteration is inefficient and makes this O(N*26) = O(N) in practice, but could be optimized. Consider maintaining a max_count variable that tracks the maximum frequency seen so far, which only needs to be updated when a character count increases. This avoids the repeated list conversion and max operation.

Suggested change
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
max_val = max(list(seen.values()))
window_len = i - start + 1
if window_len - max_val <= k:
max_count = 0
for i, char in enumerate(s):
seen[char] = seen.get(char, 0) + 1
max_count = max(max_count, seen[char])
# what I wrote initially:
# if char not in seen:
# seen[char] = 1
# else:
# seen[char] += 1
window_len = i - start + 1
if window_len - max_count <= k:

Copilot uses AI. Check for mistakes.
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.



32 changes: 32 additions & 0 deletions longest-substring-without-repeating-characters/imgolden77.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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.

class Solution:
Comment on lines +3 to +19
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 slice operation 'h[h.index(i)+1:]' combined with h.index(i) results in O(n) time complexity for each iteration, making the overall algorithm O(n^2) despite using a more sophisticated approach than simple list membership. The second solution with the dictionary avoids this issue entirely.

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:
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)
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.
#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:
class Solution:

Copilot uses AI. Check for mistakes.
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.

The membership check 'i not in h' on a list has O(n) time complexity, making the overall algorithm O(n^2) as noted in the comment. The second solution using a dictionary is more efficient. Consider removing the first solution or clearly marking it as a suboptimal approach if keeping it for educational purposes.

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.
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
Comment on lines +8 to +11
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.

There's a bug in the bit reversal logic. The code shifts left 32 times (once per iteration) and then shifts right once at the end, resulting in one extra left shift. This means the result is shifted left by 1 position more than it should be. The loop should either not shift on the last iteration, or the final right shift should be removed and the shift should happen after adding the bit rather than before.

Suggested change
res = res + bit
res = res << 1
n = n >> 1
res = res >> 1
res = (res << 1) + bit
n = n >> 1

Copilot uses AI. Check for mistakes.

return res

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

20 changes: 20 additions & 0 deletions reverse-linked-list/imgolden77.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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 if-else block contains duplicated logic. The operations inside both branches are nearly identical (post = curr.next, prev = curr, curr = post), with the only difference being curr.next assignment. This duplication can be eliminated by removing the conditional check for prev and always performing the same operations, since setting curr.next = prev works correctly even when prev is None (which is the desired behavior for the first node).

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.
57 changes: 57 additions & 0 deletions valid-parentheses/imgolden77.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# First try O(n) time complexity
class Solution:
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:
class SolutionV1:

Copilot uses AI. Check for mistakes.
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

# 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]:
return False
stack.pop()
else:
return False

return not stack
Loading