← Back to challenges

Words Inside Words

PythonHardarraysconditions

Instructions

Words can be grouped together when one word can be found within another (e.g. eat is in heat and theater). Given a list of words, return a dictionary that groups together each word with a list of all other words that contain it. Each group of words should be returned in alphabetical order.

Examples

word_groups(["grates", "rat", "rates", "rations"]) ➞ {
  "rates": ["grates"],
  "rat": ["grates", "rates", "rations"]
}

word_groups(["duct", "produce", "product", "rod"]) ➞ {
  "duct": ["product"],
  "rod": ["produce", "product"]
}

word_groups(["her", "the", "here", "other", "there"]) ➞ {
  "the": ["other", "there"],
  "here": ["there"],
  "her": ["here", "other", "there"]
}

Notes

Words can belong to more than one group.

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.