HashMap, LeetCode

242. Valid Anagram

class Solution:
    def validAnagram(self, s: str, t: str) -> bool:
        hashmap = {}
        for char in s:
            hashmap[char] += hashmap.get(char, 0)
        
        for char in t:
            if char not in hashmap:
                return False
            hashmap[char] -= hashmap.get(char, 0)
            if hashmap[char] < 0:
                return False
        return True

349. Intersection of Two Arrays

class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
        result = []
        hashmap = {}
        for num in nums1:
            if num not in hashmap:
                hashmap[num] = 1
        
        for num in nums2:
            if num in hashmap and hashmap[num] == 1:
                result.append(num)
                hashmap[num] -= 1
        return result
# find the intersection: list(set(nums1) & set(nums2))

1. Two Sum

from typing import List
def twoSum(self, nums: List[int], target: int) -> List[int]:
    hashmap = {}
    for i in range(nums):
        comp = target - nums[i]
        if comp in hashmap:
            return [hashmap[comp], i]
        else:
            hashmap[comp] = i

454. 4Sum II

Return the number of tuple, instead of listing all tuple

from typing import List
class Solution:
    def fourSumCount(self, nums1: List[int], num2: List[int], num3: List[int], num4: List[int]) -> int:
        hashmap = {}
        count = 0
        for a in nums1:
            for b in nums2:
                _sum = a + b
                hashmap[_sum] = hashmap.get(_sum, 0) + 1
        
        for c in nums3:
            for d in nums4:
                target = -(c + d)
                if target in hashmap:
                    count += hashmap[target]
        return target

18. 4Sum

from typing import List
class Solution:
    def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
        nums.sort()
        result = []
        for i in range(len(nums) - 3):
            if i > 0 and nums[i] == nums[i - 1]:
                continue
            for j in range(i + 1, len(nums) - 2):
                if j > i + 1 and nums[j] == nums[j - 1]:
                    continue
                left = j + 1
                right = len(nums) - 1
                while left < right:
                    _sum = nums[i] + nums[j] + nums[left] + nums[right]
                    if _sum > target:
                        right -= 1
                    elif _sum < target:
                        left += 1
                    else:
                        result.append([nums[i], nums[j], nums[left], nums[right]])
                        while left < right and nums[left] == nums[left + 1]:
                            left += 1
                        while left < right and nums[right] == nums[right - 1]:
                            right -= 1
                        left += 1
                        right -= 1
        return result



Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • Binary Tree, LeetCode
  • NVIDIA, LeetCode
  • Stack and Queue, LeetCode
  • String, LeetCode
  • Mock Interview(1), LeetCode