Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def helper(words: Set[str], chain: str, length: int, result: Set[str]):
- chain = chain.lstrip("-")
- if len(chain.split("-")) == length:
- result.add(chain)
- return result
- for word in words:
- if chain == "":
- helper(words, chain + "-" + word, length, result)
- continue
- if word not in chain.split("-"):
- if chain.split("-")[-1][-1] == word[0]:
- helper(words, chain + "-" + word, length, result)
- if len(chain.split("-")) == length:
- result.add(chain)
- return result
- def word_chain(words: Set[str], length: int) -> Set[str]:
- return helper(words, "", length, set())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement