Skip to content
Open
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
18 changes: 18 additions & 0 deletions LeetCode/easy/reverse_string_344.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# https://leetcode.com/problems/reverse-string/description/
# 344. Reverse String
from typing import List


class Solution:
def swap(self, x, y):
return y, x

def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
left, right = 0, len(s) - 1
while right > left:
s[left], s[right] = self.swap(s[left], s[right])
left += 1
right -= 1
37 changes: 34 additions & 3 deletions algorithms/python/array_hashing.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import heapq
from collections import Counter
from typing import List


Expand Down Expand Up @@ -29,7 +31,36 @@ def groupAnagrams(self, strs: List[str]) -> List[List[str]]:

return list(hash_map.values())

def topKFrequent(self, nums: List[int], k: int) -> List[int]:
counter = {}
for num in nums:
counter[num] = counter.get(num, 0) + 1

strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
sol = Solution().groupAnagrams(strs=strs)
print("sol: ", sol)
sorted_encounter: dict = dict(
sorted(
counter.items(),
key=lambda item: item[1],
reverse=True,
)[:k]
)
return list(sorted_encounter.keys())

def topKFrequentHeap(self, nums: List[int], k: int) -> List[int]:
freq_map = Counter(nums)
heap = []

for key, value in freq_map.items():
heapq.heappush(heap, (value, key))
if len(heap) > k:
heapq.heappop(heap)

res = []
for num in heap:
res.append(num[1])
return res


nums = [3, 0, 1, 0]
k = 1
sol = Solution().topKFrequentHeap(nums=nums, k=k)
print("topKFrequent: ", sol)
Empty file added daily_task/nov_17_2025.py
Empty file.
52 changes: 34 additions & 18 deletions tests/test_leetcode_easy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,26 @@
"num1,n,num2,m,expected",
[
# Test case 1: Normal merge
([1,2,3,0,0,0], 3, [2,5,6], 3, [1,2,2,3,5,6]),

([1, 2, 3, 0, 0, 0], 3, [2, 5, 6], 3, [1, 2, 2, 3, 5, 6]),
# Test case 2: num1 is empty except for zeroes
([0], 0, [1], 1, [1]),

# Test case 3: num2 is empty, num1 should remain the same
([1], 1, [], 0, [1]),

# Test case 4: All elements in num2 are smaller
([4,5,6,0,0,0], 3, [1,2,3], 3, [1,2,3,4,5,6]),

([4, 5, 6, 0, 0, 0], 3, [1, 2, 3], 3, [1, 2, 3, 4, 5, 6]),
# Test case 5: All elements in num2 are larger
([1,2,3,0,0,0], 3, [4,5,6], 3, [1,2,3,4,5,6]),

([1, 2, 3, 0, 0, 0], 3, [4, 5, 6], 3, [1, 2, 3, 4, 5, 6]),
# Test case 6: num1 and num2 have same values
([2,2,3,0,0,0], 3, [2,2,3], 3, [2,2,2,2,3,3]),

([2, 2, 3, 0, 0, 0], 3, [2, 2, 3], 3, [2, 2, 2, 2, 3, 3]),
# Test case 7: One list contains all duplicates
([1,1,1,0,0,0], 3, [1,1,1], 3, [1,1,1,1,1,1]),

([1, 1, 1, 0, 0, 0], 3, [1, 1, 1], 3, [1, 1, 1, 1, 1, 1]),
# Test case 8: Large input where num2 fills all of num1
([0,0,0], 0, [1,2,3], 3, [1,2,3]),

([0, 0, 0], 0, [1, 2, 3], 3, [1, 2, 3]),
# Test case 9: Already merged
([1,2,3], 3, [], 0, [1,2,3]),

([1, 2, 3], 3, [], 0, [1, 2, 3]),
# Test case 10: Mix of negative numbers
([-1,0,0,3,3,3,0,0,0], 6, [1,2,2], 3, [-1,0,0,1,2,2,3,3,3]),
]
([-1, 0, 0, 3, 3, 3, 0, 0, 0], 6, [1, 2, 2], 3, [-1, 0, 0, 1, 2, 2, 3, 3, 3]),
],
)
def test_merge_sorted_array_88(num1, n, num2, m, expected):
"""
Expand Down Expand Up @@ -98,3 +89,28 @@ def test_max_profit(prices, expected):

solution = Solution()
assert solution.maxProfit(prices) == expected


# https://leetcode.com/problems/reverse-string/description/
# 344. Reverse String
@pytest.mark.parametrize(
"s, expected",
[
(["h", "e", "l", "l", "o"], ["o", "l", "l", "e", "h"]),
(["H", "a", "n", "n", "a", "h"], ["h", "a", "n", "n", "a", "H"]),
(["A", "b", "c", "d", "e"], ["e", "d", "c", "b", "A"]),
(["a", "b", "c", "d", "e"], ["e", "d", "c", "b", "a"]),
(["a", "b", "c", "d", "e", "f"], ["f", "e", "d", "c", "b", "a"]),
(["a", "b", "c", "d", "e", "f", "g"], ["g", "f", "e", "d", "c", "b", "a"]),
(
["a", "b", "c", "d", "e", "f", "g", "h"],
["h", "g", "f", "e", "d", "c", "b", "a"],
),
],
)
def test_reverse_string_344(s, expected):
from LeetCode.easy.reverse_string_344 import Solution

solution = Solution()
solution.reverseString(s) # should return None
assert s == expected # check in-place modification
Loading