diff options
| author | TheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com> | 2024-09-24 04:50:56 +0900 |
|---|---|---|
| committer | TheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com> | 2024-09-24 04:50:56 +0900 |
| commit | bfee35a21c60e062c0033ba4e7e032b86dcadf7c (patch) | |
| tree | 2767d6dc45ff8b13a5b4cc6a790f5b38bd038268 | |
| parent | 8a77bd0cf92a726f186cd7490f7ed6a94b936fab (diff) | |
Init
| -rw-r--r-- | 1_array_hashing/top_k_elements.py | 2 | ||||
| -rw-r--r-- | 1_array_hashing/two_sum.py | 8 | ||||
| -rw-r--r-- | 1_array_hashing/valid_anagram.py | 20 |
3 files changed, 18 insertions, 12 deletions
diff --git a/1_array_hashing/top_k_elements.py b/1_array_hashing/top_k_elements.py index 13833f2..2539caa 100644 --- a/1_array_hashing/top_k_elements.py +++ b/1_array_hashing/top_k_elements.py @@ -50,7 +50,7 @@ class Solution: List[int]: elements list """ count = {} - freq = [[] for i in range(len(nums) + 1)] + freq = [[] for _ in range(len(nums) + 1)] for n in nums: count[n] = 1 + count.get(n, 0) diff --git a/1_array_hashing/two_sum.py b/1_array_hashing/two_sum.py index 81b7278..676c32b 100644 --- a/1_array_hashing/two_sum.py +++ b/1_array_hashing/two_sum.py @@ -11,8 +11,7 @@ Return the answer with the smaller index first. Example 1: -Input: -nums = [3,4,5,6], target = 7 +Input: nums = [3,4,5,6], target = 7 Output: [0,1] @@ -44,9 +43,8 @@ class Solution: def hashmap(self, nums: List[int], target: int) -> List[int]: hm: dict = {} for i, index in enumerate(nums): - diff = target - index - if diff in hm: - return [hm[diff], i] + if target - index in hm: + return [hm[target - index], i] hm[index] = i return [-1, -1] diff --git a/1_array_hashing/valid_anagram.py b/1_array_hashing/valid_anagram.py index 3305088..57f993e 100644 --- a/1_array_hashing/valid_anagram.py +++ b/1_array_hashing/valid_anagram.py @@ -21,23 +21,31 @@ Input: s = "jar", t = "jam" Output: false +Example 3: + +Input: s = "asneaxl", t = "jinyedp" + +Output: false + Constraints: - s and t consist of lowercase English letters. """ +from typing import Dict + class Solution: def hashmap(self, s: str, t: str) -> bool: if len(s) != len(t): return False - countS: dict = {} - countT: dict = {} + cs: Dict = {} + ct: Dict = {} - for i, si in enumerate(s): - countS[si] = 1 + countS.get(si, 0) - countT[t[i]] = 1 + countT.get(t[i], 0) - return countS == countT + for i, n in enumerate(s): + cs[n] = 1 + cs.get(n, 0) + ct[t[i]] = 1 + ct.get(t[i], 0) + return cs == ct caseS1 = "racecar" |
