Skip to content

Commit b540efa

Browse files
author
Shuo
committed
A: String Matching in an Array
1 parent 4b2ab79 commit b540efa

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package problem1408
2+
3+
import "strings"
4+
5+
func stringMatching(words []string) []string {
6+
var ans []string
7+
ok := func(str string) bool {
8+
for _, word := range words {
9+
if word != str && strings.Contains(word, str) {
10+
return true
11+
}
12+
}
13+
return false
14+
}
15+
for _, word := range words {
16+
if ok(word) {
17+
ans = append(ans, word)
18+
}
19+
}
20+
return ans
21+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package problem1408
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
type testType struct {
9+
in []string
10+
want []string
11+
}
12+
13+
func TestStringMatching(t *testing.T) {
14+
tests := [...]testType{
15+
{
16+
in: []string{"mass", "as", "hero", "superhero"},
17+
want: []string{"as", "hero"},
18+
},
19+
{
20+
in: []string{"leetcode", "et", "code"},
21+
want: []string{"et", "code"},
22+
},
23+
{
24+
in: []string{"blue", "green", "bu"},
25+
want: nil,
26+
},
27+
}
28+
for _, tt := range tests {
29+
got := stringMatching(tt.in)
30+
if !reflect.DeepEqual(got, tt.want) {
31+
t.Fatalf("in: %v, got: %v, want: %v", tt.in, got, tt.want)
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)