Possible Words using given characters in Python (original) (raw)

Last Updated : 12 Nov, 2025

Given a list of words and a list of characters, print all valid words that can be formed using those characters. Repetition of characters is not allowed. For Example:

**Input: [ "go", "bat", "me", "eat", "goal", "boy", "run" ], arr = [ 'e', 'o', 'b' ,'a', 'm', 'g', 'l' ]
**Output: go, me, goal

Below are the different methods to perform this task:

Using Set and Subsets

This method converts the list of available characters to a set and checks whether each word’s characters form a subset of it.

Python `

d = ["go", "bat", "me", "eat", "goal", "boy", "run"] ch = ['e', 'o', 'b', 'a', 'm', 'g', 'l'] c1 = set(ch) res = [w for w in d if set(w).issubset(c1)] print(res)

`

Output

['go', 'me', 'goal']

**Explanation:

**Note: This method only works correctly when there is no repetition of characters, since sets do not track how many times a character appears.

Using Dictionary Count Comparison

This method counts the frequency of each character in the word and compares it with the available characters.

Python `

d = ['go', 'bat', 'me', 'eat', 'goal', 'boy', 'run'] ch = ['e', 'o', 'b', 'a', 'm', 'g', 'l']

for word in d: valid = True for c in word: if c not in ch or word.count(c) > ch.count(c): valid = False break if valid: print(word)

`

**Explanation:

Using List Filtering

This method filters words by checking each character manually without using sets or recursion.

Python `

d = ["go", "bat", "me", "eat", "goal", "boy", "run"] ch = ['e', 'o', 'b', 'a', 'm', 'g', 'l']

for w in d: if all(w.count(c) <= ch.count(c) for c in set(w)): print(w)

`

**Explanation:

Recursive Combination Approach

This method recursively builds all possible words using the given characters and prints the ones found in the dictionary.

Python `

d = ["go", "bat", "me", "eat", "goal", "boy", "run"] ch = ['e', 'o', 'b', 'a', 'm', 'g', 'l']

def fun(chars, word=''): if word in d: print(word) for c in chars: n1 = chars.copy() n1.remove(c) fun(n1, word + c)

fun(ch)

`

**Explanation: