summaryrefslogtreecommitdiff
path: root/1_array_hashing
diff options
context:
space:
mode:
authorTheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com>2024-08-08 17:46:18 +0900
committerTheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com>2024-08-08 17:46:18 +0900
commit5fde9d9492c796e7b720822b1c208113e7cb0c37 (patch)
treef13316131a13eaaddbf304d0c6105f78faa40698 /1_array_hashing
parent53469ccb323c41bc32ef783fc2a24d85333bec7e (diff)
init
Diffstat (limited to '1_array_hashing')
-rw-r--r--1_array_hashing/contains_duplicate.py79
-rw-r--r--1_array_hashing/valid_anagram.py79
2 files changed, 158 insertions, 0 deletions
diff --git a/1_array_hashing/contains_duplicate.py b/1_array_hashing/contains_duplicate.py
new file mode 100644
index 0000000..142bcca
--- /dev/null
+++ b/1_array_hashing/contains_duplicate.py
@@ -0,0 +1,79 @@
+"""
+Question
+
+Duplicate Integer
+
+Easy
+
+Given an integer array nums, return true if any value appears more than once in the array, otherwise return false.
+
+Example 1:
+
+Input: nums = [1, 2, 3, 3]
+
+Output: true
+
+Example 2:
+
+Input: nums = [1, 2, 3, 4]
+
+Output: false
+"""
+
+
+class Solution:
+ # 1. brute force
+ def brute_force(self, nums: list[int]) -> bool:
+ return False
+
+ # 2. sorting
+ def sorting(self, nums: list[int]) -> bool:
+ return False
+
+ # 3. hashset
+ def hashset(self, nums: list[int]) -> bool:
+ hs = set()
+ for i in range(len(nums)):
+ if nums[i] in hs:
+ return True
+ hs.add(nums[i])
+ return False
+
+
+case1 = [1, 2, 3, 4, 5]
+case2 = [2, 2, 3, 4, 5]
+case3 = [3, 4, 1, 6, 3]
+solution = Solution()
+print(f"hashset case1: {solution.hashset(case1)}")
+print(f"hashset case2: {solution.hashset(case2)}")
+print(f"hashset case3: {solution.hashset(case3)}")
+
+
+"""
+Solution
+
+url: https://neetcode.io/problems/duplicate-integer
+video: https://youtu.be/3OamzN90kPg
+
+1. brute force
+time: O(n^2)
+space: O(1)
+
+2. sort
+time: O(nlogn)
+space: O(1)
+
+3. hashset
+time: O(n)
+space: O(n)
+code:
+class Solution:
+ def hasDuplicate(self, nums: List[int]) -> bool:
+ hashset = set()
+
+ for n in nums:
+ if n in hashset:
+ return True
+ hashset.add(n)
+ return False
+"""
diff --git a/1_array_hashing/valid_anagram.py b/1_array_hashing/valid_anagram.py
new file mode 100644
index 0000000..1c8a1d8
--- /dev/null
+++ b/1_array_hashing/valid_anagram.py
@@ -0,0 +1,79 @@
+"""
+Question
+
+Is Anagram
+
+Easy
+
+Given two strings s and t, return true if the two strings are anagrams of each other, otherwise return false.
+
+An anagram is a string that contains the exact same characters as another string, but the order of the characters can be different.
+
+Example 1:
+
+Input: s = "racecar", t = "carrace"
+
+Output: true
+
+Example 2:
+
+Input: s = "jar", t = "jam"
+
+Output: false
+
+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 = {}, {}
+ for i in range(len(s)):
+ countS[s[i]] = 1 + countS.get(s[i], 0)
+ countT[t[i]] = 1 + countT.get(t[i], 0)
+ for c in countS:
+ if countS[c] != countT.get(c, 0):
+ return False
+ return True
+
+
+case1S = "apple"
+case1T = "plpea"
+case2S = "asneaxl"
+case2T = "yejindp"
+case3S = "iloveyou"
+case3T = "youlovei"
+
+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)}")
+
+"""
+Solution
+url: https://neetcode.io/problems/is-anagram
+video: https://youtu.be/9UtInBqnCgA
+
+1. brute force
+time: O(n)
+space: O(1)
+
+2. hashmap
+time: O(s+t)
+space: O(s+t)
+code:
+class Solution:
+ def isAnagram(self, s: str, t: str) -> bool:
+ if len(s) != len(t):
+ return False
+
+ countS, countT = {}, {}
+
+ for i in range(len(s)):
+ countS[s[i]] = 1 + countS.get(s[i], 0)
+ countT[t[i]] = 1 + countT.get(t[i], 0)
+ return countS == countT
+"""