We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- No Prefix Set
- Discussions
No Prefix Set
No Prefix Set
Sort by
recency
|
184 Discussions
|
Please Login in order to post a comment
from typing import List
class TrieNode: def init(self): self.children = {} self.is_end_of_word = False
class Trie: def init(self): self.root = TrieNode()
def noPrefix(words: List[str]) -> None: trie = Trie()
Optionally, no need for a trie here.
To keep it simple, just use a BST and make a second pass through the list. Example in Java 17:
Hint: use a data structure like a trie (a prefix tree) since the alphabet is a fixed size.
You should be able to detect during insertion whether the current word is a prefix of a word already in the data structure, or whether there are any other words which have the current word as a prefix.
Essentially you are looking for an overlap of any letter with the end of any word, so most of the code is just implementing a Trie, which is missing in Java