Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class TreeNode:
- def __init__(self, val):
- self.val = val
- self.counter = 1
- self.children = []
- class Solution:
- # @param A : list of strings
- # @return a list of strings
- def prefix(self, A):
- head = TreeNode('')
- for s in A:
- n = head
- for c in s:
- found = False
- for child in n.children:
- if child.val == c:
- found = True
- n = child
- n.counter += 1
- break
- if not found:
- newChild = TreeNode(n.val + c)
- n.children.append(newChild)
- n = newChild
- stack = [head]
- result = []
- while stack:
- node = stack.pop()
- print(node.val)
- if node.counter == 1:
- result.append(node.val)
- else:
- for child in node.children:
- stack.append(child)
- return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement