242. Valid Anagram
Given two strings s and t, return true if t is an anagram of s, and false otherwise.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example 1:
Input: s = “anagram”, t = “nagaram”
Output: true
Example 2:
Input: s = “rat”, t = “car”
Output: false
Sol1: dictionary
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
gram_dict_s={}
gram_dict_t={}
for x in s:
gram_dict_s[x] = gram_dict_s[x]+1 if x in gram_dict_s else 1
for x in t:
gram_dict_t[x] = gram_dict_t[x]+1 if x in gram_dict_t else 1
return gram_dict_s == gram_dict_t
- Another version:
class Solution:
def isAnagram(self, s,t):
dict_s,dict_t={},{}
for x in s:
dict_s[x] = dict_s.get(x,0)+1
for x in t:
dict_t[x] = dict_t.get(x,0)+1
return dict_s==dict_t
- Note: dict.get(x,val): if x not in dict, assign value val; if in, get dict[x]
Sol2: counter
class Solution(object):
def isAnagram(self, s: str, t: str) -> bool:
from collections import Counter
a_count = Counter(s)
b_count = Counter(t)
return a_count == b_count
- O(n)
====================================
349. Intersection of Two Arrays
Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.
Example 1:
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]
Sol 1: hash table
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
res=set()
dict_nums1={}
for x in nums1:
dict_nums1[x]=1
for x in nums2:
if x in dict_nums1:
res.add(x)
return list(res)
- Note: “set.add(val)”
Sol2: set only
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
set_1,set_2=set(nums1),set(nums2)
return list(set_1 & set_2)
-O(N)
- Note:
- set intersection:&
- set union: |
- set different: -
====================================
202. Happy Number
Write an algorithm to determine if a number n is happy.
A happy number is a number defined by the following process:
Starting with any positive integer, replace the number by the sum of the squares of its digits.
Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
Those numbers for which this process ends in 1 are happy.
Return true if n is a happy number, and false if not.
Sol:
Version1:
class Solution:
def isHappy(self, n: int) -> bool:
val_set = set()
while n != 1:
n=sum(int(i)**2 for i in str(n))
if n in val_set:
return False
val_set.add(n)
return True
- Note: sum(int(i)**2 for i in str(n))
Version2:
class Solution2:
def isHappy(self, n: int) -> bool:
val_set = set()
def calc(n):
sum_digit=0
while n>0:
digit = n%10
n=n//10
sum_digit+=digit**2
return sum_digit
while n != 1:
x=calc(n)
if x not in val_set:
val_set.add(x)
n=x
else:
return False
return True
1. Two Sum
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
Example 1:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Input: nums = [3,3], target = 6
Output: [0,1]
Sol
- Version 1: two passes
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
tab = {}
for i in range(len(nums)):
tab[nums[i]]=i #if repeat value, updated by the last one
print(tab)
for i in range(len(nums)):
x=nums[i]
y=target-x
if y in tab and tab[y] !=i: # handle repeat value
return [i,tab[y]]
- Note:
-
- pay attention on repeat values
- when fill in table, only store the last value
- when check target, since it traverses from left, so i is left, tab[y] is the last one, they are different if target is true. Otherwise, it means only one value not two, make it not correct.
-
- Version 2: one passes
class Solution:
def twoSum(self, nums, target):
tab={}
for i in range(len(nums)):
num_2=target-nums[i]
if num_2 in tab:
return [i,tab[num_2]]
tab[nums[i]]=i
- Note: how it works in one pass: only check prior values already in tab
- example: [3,2,4], target=6
- when fill in to table 4,
- 3, and 2 are already in table
- so check 6-4=2, 2 is there