-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package day11 | ||
|
||
// Trie represents a prefix tree. | ||
type Trie struct { | ||
letters map[rune]*Trie | ||
wordCount int | ||
} | ||
|
||
// NewTrie constructs a new trie with the given words. | ||
func NewTrie(words []string) *Trie { | ||
root := &Trie{letters: make(map[rune]*Trie)} | ||
for _, word := range words { | ||
current := root | ||
for _, letter := range word { | ||
if n, found := current.letters[letter]; !found { | ||
current.letters[letter] = &Trie{letters: make(map[rune]*Trie)} | ||
current = current.letters[letter] | ||
} else { | ||
current = n | ||
} | ||
} | ||
current.wordCount++ | ||
} | ||
return root | ||
} | ||
|
||
// Match returns a list of words that match the given prefix. | ||
func (t *Trie) Match(prefix string) []string { | ||
var result []string | ||
current := t | ||
for _, r := range prefix { | ||
n, found := current.letters[r] | ||
if !found { | ||
return result // prefix not found anywhere | ||
} | ||
current = n | ||
} | ||
return append(result, dfs(current, prefix)...) | ||
} | ||
|
||
func dfs(trie *Trie, prefix string) []string { | ||
var words []string | ||
if len(trie.letters) == 0 { | ||
return []string{prefix} | ||
} | ||
for letter, node := range trie.letters { | ||
words = append(words, dfs(node, prefix+string(letter))...) | ||
} | ||
if trie.wordCount > 0 { | ||
words = append(words, prefix) | ||
} | ||
return words | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package day11 | ||
|
||
import ( | ||
"reflect" | ||
"sort" | ||
"testing" | ||
) | ||
|
||
var testcases = []struct { | ||
input []string | ||
query string | ||
expected []string | ||
}{ | ||
{[]string{"dog", "deer", "deal"}, "de", []string{"deal", "deer"}}, | ||
{[]string{"dog", "deer", "deal"}, "a", nil}, | ||
{[]string{"dog", "deer", "doggone"}, "dog", []string{"dog", "doggone"}}, | ||
{[]string{"abc", "abcd", "adec", "bde"}, "bde", []string{"bde"}}, | ||
} | ||
|
||
func TestTrie(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
trie := NewTrie(tc.input) | ||
result := trie.Match(tc.query) | ||
sort.Strings(result) | ||
if !reflect.DeepEqual(tc.expected, result) { | ||
t.Errorf("Expected %v by got %v for query %v", tc.expected, result, tc.query) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkTrie(b *testing.B) { | ||
tries := make([]*Trie, len(testcases)) | ||
for i := range tries { | ||
tries[i] = NewTrie(testcases[i].input) | ||
} | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
index := i % len(tries) | ||
tries[index].Match(testcases[index].query) | ||
} | ||
} |