-
-
Save ncaron/557f9b952ed02f86de034b20da09746d to your computer and use it in GitHub Desktop.
Substrings solution from The Odin Project
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
def substrings(text, dictionary) | |
text = text.downcase | |
count = Hash.new(0) | |
dictionary.each do |word| | |
count[word] += text.scan(word.downcase).count | |
count.delete(word) unless count[word] > 0 | |
end | |
count | |
end | |
p substrings( | |
"below", | |
["below", "down", "go", "going", "horn", "how", "howdy", "it", "i", "low", "own", "part", "partner", "sit"] | |
) == { "below" => 1, "low" => 1 } | |
p substrings( | |
"Howdy partner, sit down! How's it going?", | |
["below", "down", "go", "going", "horn", "how", "howdy", "it", "i", "low", "own", "part", "partner", "sit"] | |
) == { "down" => 1, "how" => 2, "howdy" => 1, "go" => 1, "going" => 1, "it" => 2, "i" => 3, "own" => 1, | |
"part" => 1, "partner" => 1, "sit" => 1 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment