Hashing¶
Table of Contents¶
- 760. Find Anagram Mappings (Easy) 👑
- 266. Palindrome Permutation (Easy) 👑
- 734. Sentence Similarity (Easy) 👑
- 1165. Single-Row Keyboard (Easy) 👑
- 249. Group Shifted Strings (Medium) 👑
- 1133. Largest Unique Number (Easy) 👑
- 1426. Counting Elements (Easy) 👑
- 1198. Find Smallest Common Element in All Rows (Medium) 👑
760. Find Anagram Mappings¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array, hash table
266. Palindrome Permutation¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: hash table, string, bit manipulation
266. Palindrome Permutation - Python Solution
from collections import defaultdict
# Hash
def canPermutePalindromeDict(s: str) -> bool:
if len(s) == 1:
return True
count = defaultdict(int)
for ch in s:
if count[ch] == 1:
count[ch] = 0
continue
count[ch] = 1
return sum(count.values()) <= 1
# Set
def canPermutePalindromeSet(s: str) -> bool:
if len(s) == 1:
return True
seen = set()
for ch in s:
if ch in seen:
seen.remove(ch)
else:
seen.add(ch)
return len(seen) <= 1
assert canPermutePalindromeDict("carerac") is True
assert canPermutePalindromeSet("carerac") is True
734. Sentence Similarity¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array, hash table, string
734. Sentence Similarity - Python Solution
# Hash Set
def areSentencesSimilar(sentence1, sentence2, similarPairs):
if len(sentence1) != len(sentence2):
return False
sim = set(map(tuple, similarPairs))
for i in range(len(sentence1)):
s1, s2 = sentence1[i], sentence2[i]
if s1 == s2 or (s1, s2) in sim or (s2, s1) in sim:
continue
return False
return True
if __name__ == "__main__":
sentence1 = ["great", "acting", "skills"]
sentence2 = ["fine", "drama", "talent"]
similarPairs = [
["great", "fine"],
["drama", "acting"],
["skills", "talent"],
]
print(areSentencesSimilar(sentence1, sentence2, similarPairs)) # True
1165. Single-Row Keyboard¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: hash table, string
249. Group Shifted Strings¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, hash table, string
1133. Largest Unique Number¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array, hash table, sorting
1426. Counting Elements¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array, hash table
1198. Find Smallest Common Element in All Rows¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, hash table, binary search, matrix, counting