Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. def helper(words: Set[str], chain: str, length: int, result: Set[str]):
  2.  
  3. chain = chain.lstrip("-")
  4.  
  5. if len(chain.split("-")) == length:
  6. result.add(chain)
  7. return result
  8.  
  9. for word in words:
  10.  
  11. if chain == "":
  12. helper(words, chain + "-" + word, length, result)
  13. continue
  14.  
  15. if word not in chain.split("-"):
  16. if chain.split("-")[-1][-1] == word[0]:
  17. helper(words, chain + "-" + word, length, result)
  18.  
  19. if len(chain.split("-")) == length:
  20. result.add(chain)
  21.  
  22. return result
  23.  
  24.  
  25. def word_chain(words: Set[str], length: int) -> Set[str]:
  26. return helper(words, "", length, set())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement