
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Print all valid words that are possible using Characters of Array in C++
Ib this problem, we are given a set of words and an array of character and we have to check if the words are possible using the characters of the array.
Let’s take an example to understand the problem better −
Input : words[] : {‘go’ , ‘hi’ , ‘run’ , ‘on’ , ‘hog’ , ‘gone’} Char[] : {‘a’ , ‘o’ , ‘h’ , ‘g’} Output : go , hog.
Explanation − Out of the words, the words that contain the given characters are - go, hog and rest don’t include characters in the char array.
To solve this problem, we will use the trie data structure. In this trie, we will store all the words and then search the words in trie based on the letters of the array.
Example
#include<bits/stdc++.h> using namespace std; #define char_int(c) ((int)c - (int)'a') #define int_to_char(c) ((char)c + (char)'a') struct TrieNode{ TrieNode *Child[26]; bool leaf; }; TrieNode *getNode(){ TrieNode * newNode = new TrieNode; newNode->leaf = false; for (int i =0 ; i< 26 ; i++) newNode->Child[i] = NULL; return newNode; } void insertnode(TrieNode *root, char *Key){ int n = strlen(Key); TrieNode * pChild = root; for (int i=0; i<n; i++){ int index = char_int(Key[i]); if (pChild->Child[index] == NULL) pChild->Child[index] = getNode(); pChild = pChild->Child[index]; } pChild->leaf = true; } void vaidword(TrieNode *root, bool Hash[], string str){ if (root->leaf == true) cout << str << "\t" ; for (int K =0; K < 26; K++){ if (Hash[K] == true && root->Child[K] != NULL ){ char c = int_to_char(K); vaidword(root->Child[K], Hash, str + c); } } } void PrintAllWords(char Arr[], TrieNode *root, int n){ bool Hash[26]; for (int i = 0 ; i < n; i++) Hash[char_int(Arr[i])] = true; TrieNode *pChild = root ; string str = ""; for (int i = 0 ; i < 26 ; i++){ if (Hash[i] == true && pChild->Child[i] ){ str = str+(char)int_to_char(i); vaidword(pChild->Child[i], Hash, str); str = ""; } } } int main(){ char Dict[][20] = {"go" , "hi" , "run" , "on" , "hog" , "gone"} ; TrieNode *root = getNode(); int n = sizeof(Dict)/sizeof(Dict[0]); for (int i=0; i<n; i++) insertnode(root, Dict[i]); char arr[] = {'a', 'o', 'g', 'h'} ; int N = sizeof(arr)/sizeof(arr[0]); cout<<"The words which are valid\t"; PrintAllWords(arr, root, N); return 0; }
Output
The words which are valid go hog
- Related Articles
- Python program to print Possible Words using given characters
- Print all possible words from phone digits in C++
- Print all possible strings of length k that can be formed from a set of n characters in C++
- Possible Words using given characters in Python
- Print all possible combinations of r elements in a given array of size n in C++
- What are all the possible C# array initialization syntaxes?\n
- Print all possible sums of consecutive numbers with sum N in C++
- Print all possible expressions that evaluate to a target in C++
- Print all permutations with repetition of characters in C++
- Print all possible strings that can be made by placing spaces in C++
- Queries to Print All the Divisors of n using C++
- Print all funny words in a string in C++
- Print all triplets in sorted array that form AP in C++
- Print all distinct characters of a string in order in C++
- Count of words that are present in all the given sentences in C++

Advertisements