I'm sorry if a similar problem has been asked, I could not find it.
I need to check whether string_a
contains all the characters from string_b
including the non-unique ones.
Example 1:
... string_a = 'baba'
... string_b = 'baaa'
... <solution here>
False
Example 2 (returns True
because now string_a
has enough 'a'
s):
... string_a = 'ababa'
... string_b = 'baaa'
... <solution here>
True
I tried set()
method but it only works if the characters of the strings are unique. So I have this:
... string_a = 'baba'
... string_b = 'baaa'
... return set(string_b) <= set(string_a)
True
I want it to be False
because string_b
has three 'a'
s and string_a
only two.
What you're doing is interpreting the strings as multisets and checking if one is a subset of the other. Python's multiset representation is collections.Counter
:
>>> from collections import Counter
>>> Counter('baba')
Counter({'b': 2, 'a': 2})
>>> Counter('baaa')
Counter({'a': 3, 'b': 1})
Unfortunately, Counter
doesn't implement a is_subset
method, so we have to write our own. Here are two ways to do it:
def is_sub_multiset(haystack, needle):
haystack = Counter(haystack)
needle = Counter(needle)
return not needle - haystack
# OR
# return all(haystack[elem] >= count for elem, count in needle.items())
>>> is_sub_multiset('baba', 'baaa')
False
>>> is_sub_multiset('ababa', 'baaa')
True
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With