331. Trie-Based Dynamic Multilingual Dictionary with Prefix Queries
Trie-Based Dynamic Multilingual Dictionary with Prefix Queries
Design a multilingual dictionary that stores words together with their language. The dictionary may contain multiple occurrences of the same word, including occurrences associated with different languages.
Support inserting, updating, and removing individual occurrences. Also support counting a word's total frequency and finding distinct words that begin with a given prefix.

Class Definition

MultilingualDictionary()

Method Signatures

Insert a Word

void insertWord(String word, String language)
  • Adds one occurrence of word for language.
  • Repeated insertions are stored as separate occurrences.

Update a Word

boolean updateWord(String oldWord, String newWord, String language)
  • Replaces one occurrence of oldWord associated with language with newWord.
  • Returns true when an occurrence is updated, or false when no matching occurrence exists.
  • If oldWord and newWord are equal, the method returns true when the specified occurrence exists, without changing the dictionary.

Remove a Word

boolean removeWord(String word, String language)
  • Removes one occurrence of word associated with language.
  • Returns true when an occurrence is removed, or false when no matching occurrence exists.

Count a Word

int countWord(String word)
  • Returns the total number of occurrences of word across all languages.
  • Returns 0 when the word is not stored.

Find Words by Prefix

List<String> findWordsByPrefix(String prefix)
  • Returns every distinct stored word that begins with prefix.
  • Each matching word appears only once, regardless of its frequency.
  • Words are returned in lexicographically increasing order.
  • An empty prefix returns all distinct words.

Rules

  • Words and languages are case-sensitive.
  • A word may be associated with more than one language.
  • Removing a word does not remove longer words that share its prefix.
  • Updating or removing an occurrence does not affect other occurrences.

Constraints

  • 1 ≤ word.length(), newWord.length() ≤ 100
  • 0 ≤ prefix.length() ≤ 100
  • 1 ≤ language.length() ≤ 50
  • Words and prefixes contain letters from supported languages and do not contain spaces.
  • At most 100,000 method calls are made for one dictionary.

Examples

Example 1

MultilingualDictionary()
insertWord(word = "radio", language = "English")
insertWord(word = "radio", language = "Spanish")
insertWord(word = "radiology", language = "English")
countWord(word = "radio")2
findWordsByPrefix(prefix = "radi")["radio", "radiology"]
updateWord(oldWord = "radio", newWord = "radios", language = "Spanish")true
countWord(word = "radio")1
findWordsByPrefix(prefix = "radio")["radio", "radiology", "radios"]

Example 2

MultilingualDictionary()
insertWord(word = "app", language = "English")
insertWord(word = "apple", language = "English")
insertWord(word = "application", language = "English")
removeWord(word = "app", language = "English")true
findWordsByPrefix(prefix = "app")["apple", "application"]
removeWord(word = "app", language = "English")false
countWord(word = "app")0


Please use Laptop/Desktop or any other large screen to add/edit code.