Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c760d56

Browse files
committed
Add: DI String Match
1 parent 94f317b commit c760d56

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
11
package di_string_match
2+
3+
func diStringMatch(S string) []int {
4+
lo, hi := 0, len(S)
5+
ans := make([]int, hi, hi+1)
6+
for i, v := range S {
7+
if v == 'I' {
8+
ans[i] = lo
9+
lo++
10+
} else {
11+
ans[i] = hi
12+
hi--
13+
}
14+
}
15+
return append(ans, lo)
16+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,34 @@
11
package di_string_match
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
type caseType struct {
9+
input string
10+
expected []int
11+
}
12+
13+
func TestDiStringMatch(t *testing.T) {
14+
tests := [...]caseType{
15+
{
16+
input: "IDID",
17+
expected: []int{0, 4, 1, 3, 2},
18+
},
19+
{
20+
input: "III",
21+
expected: []int{0, 1, 2, 3},
22+
},
23+
{
24+
input: "DDI",
25+
expected: []int{3, 2, 0, 1},
26+
},
27+
}
28+
for _, tc := range tests {
29+
output := diStringMatch(tc.input)
30+
if !reflect.DeepEqual(output, tc.expected) {
31+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
32+
}
33+
}
34+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /