Skip to content

Commit fb4d45e

Browse files
committed
update 4.3.2023
1 parent 3b523aa commit fb4d45e

16 files changed

+564
-3
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class TwoSum(object):
2+
3+
def __init__(self):
4+
self.hashmap = dict()
5+
6+
def add(self, number):
7+
"""
8+
:type number: int
9+
:rtype: None
10+
"""
11+
if number in self.hashmap :
12+
self.hashmap[number] += 1
13+
return
14+
# ELSE
15+
self.hashmap[number] = 1
16+
17+
18+
def find(self, value):
19+
"""
20+
:type value: int
21+
:rtype: bool
22+
"""
23+
24+
for num in self.hashmap :
25+
26+
# BASE CASE : target = 2 * num
27+
if value == 2 * num :
28+
if self.hashmap[num] >= 2 :
29+
return True
30+
continue
31+
32+
if value - num in self.hashmap :
33+
return True
34+
35+
return False
36+
37+
38+
39+
40+
41+
42+
# Your TwoSum object will be instantiated and called as such:
43+
obj = TwoSum()
44+
number = 0
45+
obj.add(number)
46+
obj.add(0)
47+
param_2 = obj.find(0)
48+
49+

202. Happy Number.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
class Solution(object):
2+
def isHappy(self, n):
3+
"""
4+
:type n: int
5+
:rtype: bool
6+
"""
7+
8+
history_set = set() # map x -> f(x) = sum of square of digit of x
9+
10+
while n != 1 :
11+
12+
fn = self.f(n)
13+
14+
if fn == 1 :
15+
return True
16+
17+
if fn in history_set :
18+
return False
19+
else :
20+
history_set.add(fn)
21+
n = fn
22+
23+
return True # when n = 1, but for some reason it was not returned in while loop
24+
25+
def f(self, n) :
26+
digit_square = [int(i)**2 for i in list(str(n))]
27+
28+
return sum(digit_square)
29+
30+
31+
## SECOND SOLUTION: USING SLOW/FAST POINTER APPROACH
32+
class Solution1(object):
33+
def isHappy(self, n):
34+
"""
35+
:type n: int
36+
:rtype: bool
37+
"""
38+
if n == 1 :
39+
return True
40+
slow = self.f(n)
41+
fast = self.f(self.f(n))
42+
while True :
43+
if slow == 1 or fast == 1 :
44+
return True
45+
if slow == fast : # Therefore, exists a cycle
46+
return False
47+
slow = self.f(slow)
48+
fast = self.f(self.f(fast))
49+
50+
51+
def f(self, n) :
52+
sum_quare = 0
53+
while n > 0:
54+
sum_quare += (n % 10)** 2
55+
n = n // 10
56+
return sum_quare
57+
58+
59+
obj = Solution1()
60+
print(obj.isHappy(n= 2))

205. Isomorphic Strings.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,49 @@ def isIsomorphic(self, s, t):
2323

2424
return True
2525

26+
# 26.2.2024, code lại ý tưởng dùng domain-image
27+
class Solution1:
28+
def isIsomorphic(self, s, t):
29+
set_s = set(s)
30+
set_t = set(t)
31+
32+
map_dict = dict()
33+
34+
for i, letter in enumerate(s) :
35+
if letter not in map_dict :
36+
map_dict[letter] = t[i] # map s[i] to t[i]
37+
38+
# then create image of s using function map_dict, then compare with t
39+
image_string = ""
40+
for letter in s :
41+
image_string += map_dict[letter]
42+
43+
# TO ENSURE THAT THERE ARE NO 2 LETTER MAP TO THE SAME IMAGE LETTER
44+
if len(map_dict.values()) > len(set(map_dict.values())) :
45+
return False
46+
47+
return image_string == t
48+
49+
obj = Solution1()
50+
s = "badc"
51+
t = "baba"
52+
print(obj.isIsomorphic(s, t))
53+
54+
55+
class Solution2:
56+
# IDEA: From set A and set B with the same length ==> always find a bijective function from A to B
57+
58+
def isIsomorphic(self, s, t):
59+
set_s = set(s)
60+
set_t = set(t)
61+
zipped_set = set(zip(s, t))
62+
63+
# when len zipped_set = len set_s, it is ensure that zipped_set is a valid function
64+
# (not exist x such that f(x) have 2 values)
65+
66+
# when len zipped_set = len set_s, it is ensure that there is no two letter map to the same letter
67+
68+
# when 3 length equals, zipped_set create a bijective function from s to t
69+
return len(set_s) == len(set_t) == len(zipped_set)
70+
71+

219. Contains Duplicate II.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution(object):
2+
def containsNearbyDuplicate(self, nums, k):
3+
"""
4+
:type nums: List[int]
5+
:type k: int
6+
:rtype: bool
7+
"""
8+
# NOTE: Ensure that all element in window is different
9+
current_window = set(nums[:k])
10+
11+
n = len(nums)
12+
13+
# FIRST BASE CASE
14+
if k >=n :
15+
return len(nums) > len(set(nums))
16+
17+
18+
if len(set(current_window)) < k :
19+
return True
20+
21+
# When n >= k+1
22+
23+
for i in range(k, n) :
24+
if nums[i] in current_window :
25+
return True
26+
# ELSE
27+
28+
current_window.add(nums[i])
29+
current_window.remove(nums[i-k])
30+
31+
32+
return False
33+
34+
35+
36+
nums = [1,2,3,4,5,4,6]
37+
k = 10
38+
39+
40+
obj = Solution()
41+
print(obj.containsNearbyDuplicate(nums, k))

249. Group Shifted Strings.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution(object):
2+
def groupStrings(self, strings):
3+
"""
4+
:type strings: List[str]
5+
:rtype: List[List[str]]
6+
"""
7+
mapping = dict()
8+
mapping["a"] = []
9+
for string in strings :
10+
11+
# BASE CASE
12+
if len(string) == 1 :
13+
mapping["a"].append(string)
14+
continue
15+
16+
17+
# WHEN STRING MORE THAN 1 LETTER
18+
19+
ord_value = self.ord_tuple(string)
20+
if ord_value not in mapping :
21+
mapping[ord_value] = [string]
22+
continue
23+
24+
#ELSE
25+
mapping[ord_value].append(string)
26+
27+
# DEAL WITH BASE CASE
28+
if mapping["a"] == [] :
29+
del mapping["a"]
30+
31+
return list(mapping.values())
32+
33+
def ord_tuple(self, string) :
34+
ord_list = []
35+
first_letter = string[0]
36+
for letter in string :
37+
38+
ord_list.append( (ord(letter) - ord(first_letter)) % 26 ) # due to contraint only lowercase
39+
return tuple(ord_list)
40+
41+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution(object):
2+
def intersect(self, nums1, nums2):
3+
"""
4+
:type nums1: List[int]
5+
:type nums2: List[int]
6+
:rtype: List[int]
7+
"""
8+
dict1 = self.create_occur_dict(nums1)
9+
dict2 = self.create_occur_dict(nums2)
10+
output_dict = dict()
11+
for key in dict1 :
12+
if key not in dict2 :
13+
continue
14+
15+
output_dict[key] = min(dict1[key], dict2[key])
16+
17+
18+
return_list = []
19+
for key in output_dict :
20+
return_list += [key] * output_dict[key]
21+
return return_list
22+
23+
24+
def create_occur_dict(self, nums) :
25+
my_dict = dict()
26+
for num in nums :
27+
if num not in my_dict :
28+
my_dict[num] = 1
29+
else :
30+
my_dict[num] += 1
31+
32+
33+
return my_dict
34+

359. Logger Rate Limiter.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Logger(object):
2+
3+
def __init__(self):
4+
self.time_dict = dict()
5+
6+
7+
8+
def shouldPrintMessage(self, timestamp, message):
9+
"""
10+
:type timestamp: int
11+
:type message: str
12+
:rtype: bool
13+
"""
14+
if message not in self.time_dict :
15+
self.time_dict[message] = timestamp
16+
return True
17+
# If exist
18+
if timestamp < self.time_dict[message] + 10 : # PREVENT
19+
return False
20+
else :
21+
# IT'S OKAYYY, update timestamp
22+
self.time_dict[message] = timestamp
23+
return True
24+
25+
26+
27+
28+
29+
30+
31+
# Your Logger object will be instantiated and called as such:
32+
# obj = Logger()
33+
# param_1 = obj.shouldPrintMessage(timestamp,message)

36. Valid Sudoku.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
class Solution(object):
3+
def isValidSudoku(self, board):
4+
"""
5+
:type board: List[List[str]]
6+
:rtype: bool
7+
"""
8+
9+
row_sets = [set() for i in range(9)]
10+
column_sets = [set() for i in range(9)]
11+
grid_sets = [set() for i in range(9)]
12+
13+
for row in range(9) :
14+
for column in range(9) :
15+
ele = board[row][column]
16+
17+
if ele == "." :
18+
continue
19+
# ELSE
20+
21+
# check for unique in row, column, grid
22+
23+
if ele in row_sets[row] :
24+
return False
25+
else :
26+
row_sets[row].add(ele)
27+
28+
if ele in column_sets[column] :
29+
return False
30+
else :
31+
column_sets[column].add(ele)
32+
33+
grid_index = 3 * (row // 3 ) + column // 3
34+
35+
36+
if ele in grid_sets[grid_index] :
37+
return False
38+
else :
39+
grid_sets[grid_index].add(ele)
40+
41+
42+
return True
43+
44+
45+
46+

0 commit comments

Comments
 (0)