From 6666388bbfc5f66756464151ff894a76a25c4ebb Mon Sep 17 00:00:00 2001 From: TheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com> Date: Sun, 25 Aug 2024 01:01:55 +0900 Subject: Init --- 1_array_hashing/contains_duplicate.py | 5 +++-- 1_array_hashing/group_anagram.py | 8 ++++++-- 1_array_hashing/top_k_elements.py | 8 ++++++-- 1_array_hashing/two_sum.py | 9 +++++++-- 1_array_hashing/valid_anagram.py | 24 ++++++++++++++---------- 5 files changed, 36 insertions(+), 18 deletions(-) diff --git a/1_array_hashing/contains_duplicate.py b/1_array_hashing/contains_duplicate.py index e437e4f..1410d4d 100644 --- a/1_array_hashing/contains_duplicate.py +++ b/1_array_hashing/contains_duplicate.py @@ -20,7 +20,6 @@ Input: nums = [1, 2, 3, 4] Output: false """ - from typing import List @@ -35,7 +34,7 @@ class Solution: # 3. hashset def hashset(self, nums: List[int]) -> bool: - hs = set() + hs: set = set() for i in range(len(nums)): if nums[i] in hs: return True @@ -70,6 +69,7 @@ space: O(1) time: O(n) space: O(n) code: +```python class Solution: def hasDuplicate(self, nums: List[int]) -> bool: hashset = set() @@ -79,4 +79,5 @@ class Solution: return True hashset.add(n) return False +``` """ diff --git a/1_array_hashing/group_anagram.py b/1_array_hashing/group_anagram.py index f302afa..35b09ae 100644 --- a/1_array_hashing/group_anagram.py +++ b/1_array_hashing/group_anagram.py @@ -38,7 +38,7 @@ from typing import List class Solution: def dictionary(self, strs: List[str]) -> List[List[str]]: - res = defaultdict(List) + res = defaultdict(list) for s in strs: count = [0] * 26 for c in s: @@ -62,8 +62,12 @@ Solution url: https://neetcode.io/problems/anagram-groups video: https://youtu.be/vzdNOK2oB2E + +1. dictionary +time: +space: code: -```dictionary +```python class Solution: def groupAnagrams(self, strs: List[str]) -> List[List[str]]: ans = defaultdict(list) diff --git a/1_array_hashing/top_k_elements.py b/1_array_hashing/top_k_elements.py index e4b0e12..e1bd5b8 100644 --- a/1_array_hashing/top_k_elements.py +++ b/1_array_hashing/top_k_elements.py @@ -50,8 +50,12 @@ Solution url: url video: video + +1. +time: +space: code: -```fn_name -code +```python + ``` """ diff --git a/1_array_hashing/two_sum.py b/1_array_hashing/two_sum.py index 76ea68d..534ec87 100644 --- a/1_array_hashing/two_sum.py +++ b/1_array_hashing/two_sum.py @@ -37,7 +37,6 @@ Constraints: -10,000,000 <= target <= 10,000,000 """ - from typing import List @@ -70,7 +69,12 @@ Solution url: https://neetcode.io/problems/two-integer-sum video: https://youtu.be/KLlXCFG5TnA -code: + +1. hashmap +time: +space: +code: +```python class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: prevMap = {} # val -> index @@ -80,4 +84,5 @@ class Solution: if diff in prevMap: return [prevMap[diff], i] prevMap[n] = i +``` """ diff --git a/1_array_hashing/valid_anagram.py b/1_array_hashing/valid_anagram.py index 9b64df3..21a756e 100644 --- a/1_array_hashing/valid_anagram.py +++ b/1_array_hashing/valid_anagram.py @@ -25,12 +25,14 @@ Constraints: - s and t consist of lowercase English letters. """ + class Solution: def hashmap(self, s: str, t: str) -> bool: if len(s) != len(t): return False - countS, countT = {}, {} + countS: dict = {} + countT: dict = {} for i in range(len(s)): countS[s[i]] = 1 + countS.get(s[i], 0) @@ -38,18 +40,18 @@ class Solution: return countS == countT -case1S = "apple" -case1T = "plpea" -case2S = "asneaxl" -case2T = "yejindp" -case3S = "iloveyou" -case3T = "youlovei" +caseS1 = "racecar" +caseT1 = "carrace" +caseS2 = "jar" +caseT2 = "jam" +caseS3 = "asneaxl" +caseT3 = "jinyedp" solution = Solution() -print(f"hashmap case1: {solution.hashmap(case1S, case1T)}") -print(f"hashmap case2: {solution.hashmap(case2S, case2T)}") -print(f"hashmap case3: {solution.hashmap(case3S, case3T)}") +print(f"hashmap case1: {solution.hashmap(caseS1, caseT1)}") +print(f"hashmap case2: {solution.hashmap(caseS2, caseT2)}") +print(f"hashmap case3: {solution.hashmap(caseS3, caseT3)}") """ Solution @@ -64,6 +66,7 @@ space: O(1) time: O(s+t) space: O(s+t) code: +```python class Solution: def isAnagram(self, s: str, t: str) -> bool: if len(s) != len(t): @@ -75,4 +78,5 @@ class Solution: countS[s[i]] = 1 + countS.get(s[i], 0) countT[t[i]] = 1 + countT.get(t[i], 0) return countS == countT +``` """ -- cgit v1.2.3