Given a dictionary and a character array, print all valid words that are possible using characters from the array.
Note: Repetitions of characters is not allowed.
Examples:
Input : Dict = ["go","bat","me","eat","goal","boy", "run"]
arr = ['e','o','b', 'a','m','g', 'l']
Output : go, me, goal.
This problem has existing solution please refer Print all valid words that are possible using Characters of Array link. We will this problem in python very quickly using Dictionary Data Structure. Approach is very simple :
- Traverse list of given strings one by one and convert them into dictionary using Counter(input) method of collections module.
- Check if all keys of any string lies within given set of characters that means this word is possible to create.
# Function to print words which can be created # using given set of characters def charCount(word): dict = {} for i in word: dict[i] = dict.get(i, 0) + 1 return dict def possible_words(lwords, charSet): for word in lwords: flag = 1 chars = charCount(word) for key in chars: if key not in charSet: flag = 0 else: if charSet.count(key) != chars[key]: flag = 0 if flag == 1: print(word) if __name__ == "__main__": input = ['goo', 'bat', 'me', 'eat', 'goal', 'boy', 'run'] charSet = ['e', 'o', 'b', 'a', 'm', 'g', 'l'] possible_words(input, charSet) |
Output:
go me goal
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Print all valid words that are possible using Characters of Array
- Check if the given string of words can be formed from words present in the dictionary
- Count words that appear exactly two times in an array of words
- How to Remove repetitive characters from words of the given Pandas DataFrame using Regex?
- Python | Words extraction from set of characters using dictionary
- K length words that can be formed from given characters without repetition
- Print all possible combinations of words from Dictionary using Trie
- Python | Toggle characters in words having same case
- Python - Remove words containing list characters
- Python - Get number of characters, words, spaces and lines in a file
- Java program to swap first and last characters of words in a sentence
- Group words with same set of characters
- Print all possible words from phone digits
- Java ArrayList to print all possible words from phone digits
- Strings formed from given characters without any consecutive repeating characters
- Longest substring with atmost K characters from the given set of characters
- Check if a two character string can be made using given words
- Find words which are greater than given length k using stringstream
- Given a sequence of words, print all anagrams together using STL
- Largest palindromic string possible from given strings by rearranging the characters
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : vijaymaddukuri

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
