Skip to content

Instantly share code, notes, and snippets.

@ncaron
Created January 13, 2018 05:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ncaron/557f9b952ed02f86de034b20da09746d to your computer and use it in GitHub Desktop.
Save ncaron/557f9b952ed02f86de034b20da09746d to your computer and use it in GitHub Desktop.
Substrings solution from The Odin Project
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