Possible Words using given characters in Python
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.
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.
go me goal