Challenge: Total Number of Words in a Trie
Explore how to compute the total number of words stored in a trie data structure. Understand constraints and implement solutions to accurately count words using JavaScript, enhancing your skills in trie operations and coding interviews.
We'll cover the following...
We'll cover the following...
Statement
Given a trie data structure that represents an array of words, words, determine the total number of words stored in it.
Constraints:
words.lengthwords[i].lengthAll
words[i]consist of lowercase English alphabets
Examples
1 / 3
Try it yourself
Implement your solution in solution.js in the following code playground:
JavaScript
usercode > solution.js
import {TrieNode} from './TrieNode.js';import {Trie} from './Trie.js';/*** Create Trie => let trie = new Trie()** TrieNode => {children, isEndWord, char,* markAsLeaf(), unMarkAsLeaf()}** access root => trie.root* getIndex of character 't' => trie.getIndex(t)* t.charCodeAt(0) => The charCodeAt() function returnsthe order of a given character* Insert a Word => trie.insert(key) where key is string, and value is int* Search a Word => trie.search(key) return true or false* Delete a Word => trie.delete(key)* Note: the structure of TrieNode and Trie class is the same* as we learnt in the first two lessons. All members and methods* are available for use in this challenge*/function totalWords(rootN) {// Replace this placeholder return statement with your codereturn 0;}export {totalWords};
Click "Run" to evaluate your code.
Total Number of Words in a Trie