From d5887216fec6d1678cf55e7d9d3aa51f31714f99 Mon Sep 17 00:00:00 2001 From: wazedkhan Date: Mon, 17 Nov 2025 17:56:45 +0600 Subject: [PATCH] 344: reverse string --- LeetCode/easy/reverse_string_344.py | 18 ++++++++++ algorithms/python/array_hashing.py | 37 ++++++++++++++++++-- daily_task/nov_17_2025.py | 0 tests/test_leetcode_easy.py | 52 +++++++++++++++++++---------- 4 files changed, 86 insertions(+), 21 deletions(-) create mode 100644 LeetCode/easy/reverse_string_344.py create mode 100644 daily_task/nov_17_2025.py diff --git a/LeetCode/easy/reverse_string_344.py b/LeetCode/easy/reverse_string_344.py new file mode 100644 index 0000000..e1613d1 --- /dev/null +++ b/LeetCode/easy/reverse_string_344.py @@ -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 diff --git a/algorithms/python/array_hashing.py b/algorithms/python/array_hashing.py index da7f36b..dec2529 100644 --- a/algorithms/python/array_hashing.py +++ b/algorithms/python/array_hashing.py @@ -1,3 +1,5 @@ +import heapq +from collections import Counter from typing import List @@ -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) diff --git a/daily_task/nov_17_2025.py b/daily_task/nov_17_2025.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_leetcode_easy.py b/tests/test_leetcode_easy.py index 4bdc876..40c193e 100644 --- a/tests/test_leetcode_easy.py +++ b/tests/test_leetcode_easy.py @@ -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): """ @@ -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