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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -374,4 +374,5 @@ problems from
* [Day 376](https://github.com/vaskoz/dailycodingproblem-go/issues/758)
* [Day 377](https://github.com/vaskoz/dailycodingproblem-go/issues/760)
* [Day 378](https://github.com/vaskoz/dailycodingproblem-go/issues/762)
* [Day 379](https://github.com/vaskoz/dailycodingproblem-go/issues/764)

23 changes: 23 additions & 0 deletions day379/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package day379

// AllPossibleSubsequences returns all the possible
// ordered subsequences.
func AllPossibleSubsequences(str string) map[string]struct{} {
ansSet := make(map[string]struct{})
allPossibleSubsequences(str, ansSet)

return ansSet
}

func allPossibleSubsequences(str string, ansSet map[string]struct{}) {
if _, alreadyExists := ansSet[str]; len(str) == 0 || alreadyExists {
return
}

for i := range str {
sub := str[:i] + str[i+1:]
allPossibleSubsequences(sub, ansSet)
}

ansSet[str] = struct{}{}
}
41 changes: 41 additions & 0 deletions day379/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package day379

import (
"reflect"
"testing"
)

// nolint
var testcases = []struct {
input string
subseq map[string]struct{}
}{
{"xyz",
map[string]struct{}{
"x": {},
"y": {},
"z": {},
"xy": {},
"xz": {},
"yz": {},
"xyz": {},
}},
}

func TestAllPossibleSubsequences(t *testing.T) {
t.Parallel()

for _, tc := range testcases {
if result := AllPossibleSubsequences(tc.input); !reflect.DeepEqual(result, tc.subseq) {
t.Errorf("Expected %v, got %v", tc.subseq, result)
}
}
}

func BenchmarkAllPossibleSubsequences(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
AllPossibleSubsequences(tc.input)
}
}
}