Advertisement
Guest User

Untitled

a guest
May 21st, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. class TreeNode:
  2. def __init__(self, val):
  3. self.val = val
  4. self.counter = 1
  5. self.children = []
  6.  
  7. class Solution:
  8. # @param A : list of strings
  9. # @return a list of strings
  10. def prefix(self, A):
  11. head = TreeNode('')
  12.  
  13. for s in A:
  14. n = head
  15. for c in s:
  16. found = False
  17. for child in n.children:
  18. if child.val == c:
  19. found = True
  20. n = child
  21. n.counter += 1
  22. break
  23. if not found:
  24. newChild = TreeNode(n.val + c)
  25. n.children.append(newChild)
  26. n = newChild
  27.  
  28. stack = [head]
  29. result = []
  30. while stack:
  31. node = stack.pop()
  32. print(node.val)
  33. if node.counter == 1:
  34. result.append(node.val)
  35. else:
  36. for child in node.children:
  37. stack.append(child)
  38.  
  39. return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement