Skip to content

Commit 4df5fb5

Browse files
authored
Merge branch 'main' into main
2 parents 4f2a683 + edcf7be commit 4df5fb5

File tree

361 files changed

+113814
-985
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

361 files changed

+113814
-985
lines changed

Diff for: README.md

+488-488
Large diffs are not rendered by default.

Diff for: articles/add-two-numbers.md

+587
Large diffs are not rendered by default.

Diff for: articles/anagram-groups.md

+81-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Solution:
99
for s in strs:
1010
sortedS = ''.join(sorted(s))
1111
res[sortedS].append(s)
12-
return res.values()
12+
return list(res.values())
1313
```
1414

1515
```java
@@ -85,6 +85,46 @@ public class Solution {
8585
}
8686
```
8787

88+
```go
89+
func groupAnagrams(strs []string) [][]string {
90+
res := make(map[string][]string)
91+
92+
for _, s := range strs {
93+
sortedS := sortString(s)
94+
res[sortedS] = append(res[sortedS], s)
95+
}
96+
97+
var result [][]string
98+
for _, group := range res {
99+
result = append(result, group)
100+
}
101+
return result
102+
}
103+
104+
func sortString(s string) string {
105+
characters := []rune(s)
106+
sort.Slice(characters, func(i, j int) bool {
107+
return characters[i] < characters[j]
108+
})
109+
return string(characters)
110+
}
111+
```
112+
113+
```kotlin
114+
class Solution {
115+
fun groupAnagrams(strs: Array<String>): List<List<String>> {
116+
val res = mutableMapOf<String, MutableList<String>>()
117+
118+
for (s in strs) {
119+
val sortedS = s.toCharArray().sorted().joinToString("")
120+
res.getOrPut(sortedS) { mutableListOf() }.add(s)
121+
}
122+
123+
return res.values.toList()
124+
}
125+
}
126+
```
127+
88128
::tabs-end
89129

90130
### Time & Space Complexity
@@ -109,7 +149,7 @@ class Solution:
109149
for c in s:
110150
count[ord(c) - ord('a')] += 1
111151
res[tuple(count)].append(s)
112-
return res.values()
152+
return list(res.values())
113153
```
114154
115155
```java
@@ -199,11 +239,49 @@ public class Solution {
199239
}
200240
```
201241

242+
```go
243+
func groupAnagrams(strs []string) [][]string {
244+
res := make(map[[26]int][]string)
245+
246+
for _, s := range strs {
247+
var count [26]int
248+
for _, c := range s {
249+
count[c-'a']++
250+
}
251+
res[count] = append(res[count], s)
252+
}
253+
254+
var result [][]string
255+
for _, group := range res {
256+
result = append(result, group)
257+
}
258+
return result
259+
}
260+
```
261+
262+
```kotlin
263+
class Solution {
264+
fun groupAnagrams(strs: Array<String>): List<List<String>> {
265+
val res = HashMap<List<Int>, MutableList<String>>()
266+
267+
for (s in strs) {
268+
val count = MutableList(26) { 0 }
269+
for (c in s) {
270+
count[c - 'a']++
271+
}
272+
res.getOrPut(count) { mutableListOf() }.add(s)
273+
}
274+
275+
return res.values.toList()
276+
}
277+
}
278+
```
279+
202280
::tabs-end
203281

204282
### Time & Space Complexity
205283

206284
* Time complexity: $O(m * n)$
207285
* Space complexity: $O(m)$
208286

209-
> Where $m$ is the number of strings and $n$ is the length of the longest string.
287+
> Where $m$ is the number of strings and $n$ is the length of the longest string.

0 commit comments

Comments
 (0)