![]() |
VOOZH | about |
Prerequisite:Trie
Given a list of stringstr[] and a prefix string pre. The task is to count the number of words in the list of string with a given prefix using trie.
Examples:
Input:str = [ "apk", "app", "apple", "arp", "array" ], pre = "ap"
Output: 3
Explanation:
Below is the representation of trie from using above string.
The words in str having prefix "ap" are apk, app and apple.
So, the count is 3Input:str = [ "gee", "geek", "geezer", "geeksforgeeks", "geekiness", "geekgod" ], pre = "geek"
Output: 4
Approach:
To solve this problem Trie Data Structure is used and each node of this Trie contains the following three fields:
Steps:
Below is the implementation of the above approach:
3
Time Complexity: O(n*l) where n = the number of words inserted in Trie and l = the length of the longest word inserted in Trie.
Auxiliary Space: O(n*l).