Skip to content

Commit 169db68

Browse files
author
Shuo
authored
Merge pull request #800 from openset/develop
A: String Matching in an Array
2 parents 2eb71e2 + 76b5ee7 commit 169db68

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package problem1408
2+
3+
import "strings"
4+
5+
func stringMatching(words []string) []string {
6+
var ans []string
7+
for _, word := range words {
8+
for _, val := range words {
9+
if val != word && strings.Contains(val, word) {
10+
ans = append(ans, word)
11+
break
12+
}
13+
}
14+
}
15+
return ans
16+
}
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)