[
string
counter
hash table
]
BinarySearch 0307 Word Formation Sequel
Problem statement
https://binarysearch.com/problems/Word-Formation-Sequel/
Solution
Let us use counters here. Calculate l1
is number of *
symbols and l2
is counter of the rest symbols. Then for each w
in words, check difference of counters, and if it has <= l1
elements, we can get this word.
Complexity
It is O(n)
for time, where n
is the total length of all words and O(26)
for space.
Code
class Solution:
def solve(self, words, letters):
l1 = letters.count("*")
l2 = Counter([x for x in letters if x != "*"])
ans = 0
for w in words:
if sum(x for x in (Counter(w) - l2).values()) <= l1:
ans = max(ans, len(w))
return ans