Conversation
|
No specific feedback provided. Refer to GitHub Comment. |
|
Strengths:
Areas for Improvement:
Suggested improvement for Group Anagrams: class Solution:
def __init__(self):
self.primes = self.first_26_primes()
def first_26_primes(self):
primes = []
num = 2
while len(primes) < 26:
is_prime = True
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
is_prime = False
break
if is_prime:
primes.append(num)
num += 1
return primes
def getHash(self, key: str) -> int:
hash_val = 1
for ch in key:
hash_val *= self.primes[ord(ch) - ord('a')]
return hash_val
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
groupedStrs = defaultdict(list)
for s in strs:
key = self.getHash(s)
groupedStrs[key].append(s)
return list(groupedStrs.values())Alternatively, to avoid precision issues entirely, you can use a tuple of character counts: def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
groupedStrs = defaultdict(list)
for s in strs:
count = [0] * 26
for c in s:
count[ord(c) - ord('a')] += 1
groupedStrs[tuple(count)].append(s)
return list(groupedStrs.values())This method is also O(n * k) and avoids any precision problems. For the other problems, your solutions are good. However, for IsomorphicStrings, the array size of 100 might not be sufficient if the characters extend beyond ASCII 32 to 131. Actually, the problem states that both s and t consist of any valid ASCII character. So you should use a larger array or a dictionary. But in the problem, it says "all characters are in the ASCII set", so your solution might work if the ASCII values are within that range. However, to be safe, you can use dictionaries for both mappings. |
No description provided.