Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def findPrefixes(self, arr, N):
- '''
- while adding each character to trie
- If character is not present then create a new dictionary for that character with also an item (freq:1) and get into it.
- If character is already prsent in the current dictionary
- then increase the count of that branch i.e. freq which represents the no of words passing through
- that character.
- '''
- def add(word):
- cur=root
- for w in word:
- if w in cur:
- cur[w]['freq']+=1
- else:
- cur[w]={'freq':1}
- cur=cur[w]
- cur['*']=True
- '''
- Now traverse each character of word through trie.
- First add character to ans and check whether the frequency of that character==1 .If so return ans
- else Traverse
- '''
- def find(word):
- ans=''
- cur=root
- for w in word:
- ans=ans+w
- if cur[w]['freq']==1:
- return ans
- cur=cur[w]
- ans=[]
- root={}
- for word in arr:
- add(word)
- for word in arr:
- ans.append(find(word))
- return ans
Add Comment
Please, Sign In to add comment