Equivalent Transformation¶
Table of Contents¶
- 3375. Minimum Operations to Make Array Values Equal to K (Easy)
- 2914. Minimum Number of Changes to Make Binary String Beautiful (Medium)
- 3365. Rearrange K Substrings to Form Target String (Medium)
- 1657. Determine if Two Strings Are Close (Medium)
- 2551. Put Marbles in Bags (Hard)
- 1585. Check If String Is Transformable With Substring Sort Operations (Hard)
- 1040. Moving Stones Until Consecutive II (Medium)
- 249. Group Shifted Strings (Medium) 👑
- 49. Group Anagrams (Medium)
- 1183. Maximum Number of Ones (Hard) 👑
3375. Minimum Operations to Make Array Values Equal to K¶
2914. Minimum Number of Changes to Make Binary String Beautiful¶
3365. Rearrange K Substrings to Form Target String¶
1657. Determine if Two Strings Are Close¶
2551. Put Marbles in Bags¶
1585. Check If String Is Transformable With Substring Sort Operations¶
1040. Moving Stones Until Consecutive II¶
249. Group Shifted Strings¶
49. Group Anagrams¶
from collections import defaultdict
from typing import List
# Hash - List
def groupAnagrams(strs: List[str]) -> List[List[str]]:
result = defaultdict(list)
for s in strs:
count = [0] * 26
for i in s:
count[ord(i) - ord("a")] += 1
result[tuple(count)].append(s)
return list(result.values())
# |-------------|-----------------|--------------|
# | Approach | Time | Space |
# |-------------|-----------------|--------------|
# | Hash | O(n * k) | O(n) |
# |-------------|-----------------|--------------|
strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
print(groupAnagrams(strs))
# [['eat', 'tea', 'ate'], ['tan', 'nat'], ['bat']]