
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find length of longest word that can be formed from given letters in python
Suppose we have a list of words and a string called letters, we have to find the size of the longest word that can be made by rearranging the given letters. In the letters there may be asterisk character (*) it can match any character. And it is not necessary to use all the letters.
So, if the input is like words = ["prince", "rice", "price", "limit", "hello"] letters = "*r**ce*", then the output will be 6, as the longest word we can make is "prince" the length is 6.
To solve this, we will follow these steps:
- has := a map containing letters and frequencies of each element in letters
- Define a function valid() . This will take s
- need := a map containing letters and frequencies of each element in s
- extra := sum of all elements of (maximum of 0 and need[char] - has[char] for all char in need)
- return true when extra <= has["*"]
- From the main method do the following:
- return maximum of all elements in the list [size of word for all word in words when word is valid]
Let us see the following implementation to get better understanding:
Example
from collections import Counter class Solution: def solve(self, words, letters): has = Counter(letters) def valid(s): need = Counter(s) extra = sum([max(0, need[char] - has[char]) for char in need]) return extra <= has["*"] return max([len(word) for word in words if valid(word)]) ob = Solution() words = ["prince", "rice", "price", "limit", "hello"] letters = "*r**ce*" print(ob.solve(words, letters))
Input
["prince", "rice", "price", "limit", "hello"], "*r**ce*"
Output
6
- Related Articles
- Program to find the length of the longest, that can be made using given letters in Python
- Program to find length of longest diminishing word chain in Python?
- Program to find length of longest valid parenthesis from given string in Python
- Program to find length longest prefix sequence of a word array in Python
- Program to find length of longest alternating subsequence from a given list in Python
- Program to find length of longest Fibonacci subsequence from a given list in Python
- Program to find length of longest sublist with given condition in Python
- All possible strings of any length that can be formed from a given string?
- Python Program to return the Length of the Longest Word from the List of Words
- Program to find length of longest arithmetic subsequence of a given list in Python
- Program to find length of longest matrix path length in Python
- Program to find length of longest interval from a list of intervals in Python
- Recursively print all sentences that can be formed from list of word lists in C++
- Program to find length of longest balanced subsequence in Python
- Program to find length of longest anagram subsequence in Python

Advertisements