Ad

How Can I Check If A String Has All Characters Of A Substring?

I need to find if a string has all characters of a substring. So for a 7 char string i want to know if the chars of a 5 char substings are in the string, without repeating letters (if substring has 2 letters a, then the string needs at max 2 letters a).

For exemple :

Substring = 'aabfy'
String1 = 'aabcfgy'
String2 = 'abcfgmy' 
String3 = 'aaabcfy'

Then String1 and String3 are True, but String2 is False. Because sub string is in 1 and 3 but not in 2 (double a, but it can also be any other letter).

I hope i explained myself, any questions i can and will answer.

Ad

Answer

You can use collections.Counter in the following way:

from collections import Counter

def contains_all(string, substring):
    c1, c2 = Counter(string), Counter(substring)
    return all(c1[x] >= c2[x] for x in c2)

This will ensure every character in the substring is at least as many times in the containing string.

>>> contains_all('aaabcfy', 'aabfy')
True
>>> contains_all('abcfgmy', 'aabfy')
False
>>> contains_all('aabcfgy', 'aabfy')
True
Ad
source: stackoverflow.com
Ad