Skip to content

Instantly share code, notes, and snippets.

@everylittlefox
Created August 6, 2022 15:49
Show Gist options
  • Select an option

  • Save everylittlefox/c3142d04d577a4b12e29f25f94acbfd7 to your computer and use it in GitHub Desktop.

Select an option

Save everylittlefox/c3142d04d577a4b12e29f25f94acbfd7 to your computer and use it in GitHub Desktop.
def substrings(word, dictionary)
dictionary.reduce(Hash.new(0)) do |substr_count, substr|
idx = 0
while idx < word.length
if word[idx, substr.length] == substr
substr_count[substr] += 1
idx += substr.length
else
idx += 1
end
end
substr_count
end
end
dictionary = ["below","down","go","going","horn","how","howdy","it","i","low","own","part","partner","sit"]
p substrings("below", dictionary)
p substrings("Howdy partner, sit down! How's it going?", dictionary)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment