Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions problems/di-string-match/di_string_match.go
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
package di_string_match

func diStringMatch(S string) []int {
lo, hi := 0, len(S)
ans := make([]int, hi, hi+1)
for i, v := range S {
if v == 'I' {
ans[i] = lo
lo++
} else {
ans[i] = hi
hi--
}
}
return append(ans, lo)
}
33 changes: 33 additions & 0 deletions problems/di-string-match/di_string_match_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,34 @@
package di_string_match

import (
"reflect"
"testing"
)

type caseType struct {
input string
expected []int
}

func TestDiStringMatch(t *testing.T) {
tests := [...]caseType{
{
input: "IDID",
expected: []int{0, 4, 1, 3, 2},
},
{
input: "III",
expected: []int{0, 1, 2, 3},
},
{
input: "DDI",
expected: []int{3, 2, 0, 1},
},
}
for _, tc := range tests {
output := diStringMatch(tc.input)
if !reflect.DeepEqual(output, tc.expected) {
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
}
}
}