
- 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 count maximum number of strings we can generate from list of words and letter counts in python
Suppose we have a list of strings where each string contains two letters "A"s and "B"s. We have two values a and b. We have to find the maximum number of strings that can be formed. We can use at most a number of "A"s and at most b number of "B"s, without reuse.
So, if the input is like strings = ["AAABB", "AABB", "AA", "BB"] a = 4 b = 2, then the output will be 2, as we can take the strings using 4 "A"s and 2 "B"s ["AABB","AA"].
To solve this, we will follow these steps −
- pairs := a new list
- for each w in strings, do
- A := number of "A" in w
- B := size of w - A
- insert a pair (A, B) at the end of pairs
- ans := a map where (a, b) has value 0
- for each pait (A, B) in pairs, do
- temp := a new map from ans
- for each pair (temp_a, temp_b), and value wc of ans, do
- if temp_a >= A and temp_b >= B, then
- rem := a pait (temp_a - A, temp_b - B)
- temp[rem] := maximum of temp[rem] (if rem is not there, 0) and (wc + 1)
- ans := temp
- if temp_a >= A and temp_b >= B, then
- return maximum of list of all values of ans
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, strings, a, b): pairs = [] for w in strings: A = w.count("A") B = len(w) - A pairs.append((A, B)) ans = {(a, b): 0} for A, B in pairs: temp = dict(ans) for (temp_a, temp_b), wc in ans.items(): if temp_a >= A and temp_b >= B: rem = (temp_a - A, temp_b - B) temp[rem] = max(temp.get(rem, 0), wc + 1) ans = temp return max(ans.values()) ob = Solution() strings = ["AAABB", "AABB", "AA", "BB"] a = 4 b = 2 print(ob.solve(strings, a, b))
Input
["AAABB", "AABB", "AA", "BB"], 4, 2
Output
2
- Related Articles
- Program to count number of words we can generate from matrix of letters in Python
- Python Program to Count of Words with specific letter
- Program to count number of strings we can make using grammar rules in Python
- Program to find maximum number of coins we can collect in Python
- Program to count the number of consistent strings in Python
- Program to count number of ways we can make a list of values by splitting numeric string in Python
- Program to find number of ways we can concatenate words to make palindromes in Python
- Program to find maximum number of coins we can get using Python
- Program to find maximum number of people we can make happy in Python
- Program to count number of ways we can throw n dices in Python
- Program to count number of ways we can distribute coins to workers in Python
- Python program to count distinct words and count frequency of them
- Python program to find word score from list of words
- Python program to find uncommon words from two Strings
- Python Program to Count number of binary strings without consecutive 1’

Advertisements