-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path第五道:(49). Group Anagrams - Medium
133 lines (79 loc) · 3.23 KB
/
第五道:(49). Group Anagrams - Medium
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
Solving method: sorting, hashmap
From NeetCode:
```
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
ans = defaultdict (list) #mapping charCount to list of anagrams. defauldict it means "Defining a dictionary with values as a list."
for s in strs:
count = [0]*26 #a-z
for c in s:
count [ord(c)-ord("a")]+= 1
ans[tuple(count)].append(s)
return ans.values()
```
From LeetCode:
```python
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
anagram_map = defaultdict(list)
for word in strs:
sorted_word = ''.join(sorted(word))
anagram_map[sorted_word].append(word)
return list(anagram_map.values())
```
what I’ve learned:
- **defaultdict(list)**
**Using List as default_factory**
When the list class is passed as the default_factory argument, then a defaultdict is created with the values that are list.
`# Defining a dict`
`d =` `defaultdict(list)`
`for` `i in` `range(5):`
`d[i].append(i)`
`print("Dictionary with values as list:")`
`print(d)`
---
**Output:**
```
Dictionary with values as list:
defaultdict(<class 'list'>, {0: [0], 1: [1], 2: [2], 3: [3], 4: [4]})
```
**Using int as default_factory**
When the int class is passed as the default_factory argument, then a defaultdict is created with default value as zero.
`# Defining the dict`
`d =` `defaultdict(int)`
`L =` `[1, 2, 3, 4, 2, 4, 1, 2]`
`# Iterate through the list`
`# for keeping the count`
`for` `i in` `L:`
`# The default value is 0`
`# so there is no need to`
`# enter the key first`
`d[i] +=` `1`
`print(d)`
---
**Output:**
```
defaultdict(<class 'int'>, {1: 2, 2: 3, 3: 1, 4: 2})
```
**Example:**
- link: https://www.geeksforgeeks.org/defaultdict-in-python/
- **append**
-

- **ord**
- Definition and Usage
The `ord()` function returns the number representing the unicode code of a specified character.
---
Syntax
ord(*character*)
Parameter Values
| Parameter | Description |
| --- | --- |
| character | String, any character |
- tuple
- Tuple
Tuples are used to store multiple items in a single variable.
Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are [List](https://www.w3schools.com/python/python_lists.asp), [Set](https://www.w3schools.com/python/python_sets.asp), and [Dictionary](https://www.w3schools.com/python/python_dictionaries.asp), all with different qualities and usage.
A tuple is a collection which is ordered and **unchangeable**.
Tuples are written with round brackets.
- join