Skip to content

Commit 3a11669

Browse files
committed
Add failing text case and change the logic to pass the test
1 parent 1266c63 commit 3a11669

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

strings/longest_dictionary_word.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ func LongestDictionaryWordContainingKey(key string, dic []string) string {
1616
return longest
1717
}
1818

19-
// hash turns a string into a number
20-
// the output for "abc", "acb", "cba" and etc… are all the same.
21-
func hash(s string) rune {
22-
var res rune
23-
for _, w := range s {
24-
res |= w
19+
// hash turns a string into a number representing a bitmask of characters
20+
// the output for "abc", "acb", "cba", "aabc", "aaabbcc" and etc… are all the same.
21+
func hash(input string) int64 {
22+
var res int64
23+
for _, char := range input {
24+
res |= 1 << (char - 'a')
2525
}
2626
return res
2727
}

strings/longest_dictionary_word_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ TestLongestDictionaryWordContainingKey tests solution(s) with the following sign
88
func LongestDictionaryWordContainingKey(key string, dic []string) string
99
1010
Given a key as string, and a slice of strings containing a dictionary of words, return the longest
11-
word that contains all letters of the key.
11+
word that contains all letters of the key. Input will only contain lowercase letters a-z.
1212
1313
For example given "car" and {"rectify", "race", "archeology", "racoon"}, it should return "archeology",
1414
because "archeology" is the longest word in the given set that contains "c","a",and "r".
@@ -20,10 +20,12 @@ func TestLongestDictionaryWordContainingKey(t *testing.T) {
2020
longestWordContainingCharacters string
2121
}{
2222
{"a", []string{}, ""},
23+
{"a", []string{"c"}, ""},
2324
{"", []string{"abc"}, "abc"},
2425
{"a", []string{"a", "b", "c"}, "a"},
2526
{"a", []string{"a", "ba", "c", "cc"}, "ba"},
2627
{"a", []string{"a", "baa", "c"}, "baa"},
28+
{"abc", []string{"abc", "aabc", "aabbcc", "bbccaaccbbaa"}, "bbccaaccbbaa"},
2729
{"abc", []string{"abc", "abcdefghijklmn", "abcdefghijkl", "abcdef", "abcijkl"}, "abcdefghijklmn"},
2830
{"car", []string{"rectify", "race", "archeology", "racoon"}, "archeology"},
2931
}

0 commit comments

Comments
 (0)