Skip to content

Commit a7e4819

Browse files
committed
add sol
1 parent bf4c2d8 commit a7e4819

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

leetcode/daily/1408/sol.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)