Print all possible combinations of words from Dictionary using Trie
Given an array of strings arr, for every string in the array, print all possible combinations of strings that can be concatenated to make that word.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Input: arr = ["sam", "sung", "samsung"] Output: sam: sam sung: sung samsung: sam sung samsung String 'samsung' can be formed using two different strings from the array i.e. 'sam' and 'sung' whereas 'samsung' itself is also a string in the array. Input: arr = ["ice", "cream", "icecream"] Output: ice: ice cream: cream icecream: ice cream icecream
- Add all the given strings into trie.
- Process every prefix character by character and check if it forms a word from trie by searching.
- If the prefix is present in the trie then add it to the result and proceed further with the remaining suffix in the string.
- Once it reaches the end of the string, print all the combinations found.
Below is the implementation of the above approach:
sam: sam sung: sung samsung: sam sung samsung