Skip to content

Commit

Permalink
add sol
Browse files Browse the repository at this point in the history
  • Loading branch information
ductnn committed Jan 7, 2025
1 parent bf4c2d8 commit a7e4819
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions leetcode/daily/1408/sol.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"fmt"
"sort"
"strings"
)

func stringMatching(words []string) []string {
// Sort words by length
sort.Slice(words, func(i, j int) bool {
return len(words[i]) < len(words[j])
})

var result []string

// Check each word if it's a substring of any other word
for i, word := range words {
for _, other := range words[i+1:] {
if strings.Contains(other, word) {
result = append(result, word)
break
}
}
}

return result
}

func main() {
words := []string{"mass", "as", "hero", "superhero"}
fmt.Println(stringMatching(words))
}

0 comments on commit a7e4819

Please sign in to comment.