We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent bf4c2d8 commit a7e4819Copy full SHA for a7e4819
leetcode/daily/1408/sol.go
@@ -0,0 +1,33 @@
1
+package main
2
+
3
+import (
4
+ "fmt"
5
+ "sort"
6
+ "strings"
7
+)
8
9
+func stringMatching(words []string) []string {
10
+ // Sort words by length
11
+ sort.Slice(words, func(i, j int) bool {
12
+ return len(words[i]) < len(words[j])
13
+ })
14
15
+ var result []string
16
17
+ // Check each word if it's a substring of any other word
18
+ for i, word := range words {
19
+ for _, other := range words[i+1:] {
20
+ if strings.Contains(other, word) {
21
+ result = append(result, word)
22
+ break
23
+ }
24
25
26
27
+ return result
28
+}
29
30
+func main() {
31
+ words := []string{"mass", "as", "hero", "superhero"}
32
+ fmt.Println(stringMatching(words))
33
0 commit comments