Search code examples
pythonpython-3.xstringpython-2.7substring

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.


Solution

  • 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
    

    Update: A better version (thx to @HenadziRabkin for the hint), using Counter difference:

    def contains_all(string, substring):
        return not (Counter(substring) - Counter(string))
    
    self.cs = Counter(source)
    # more efficient version
    def countains_all(target):
        ct = Counter(target)
        for k,v in ct.items(): # Compare only target symbols
           if k not in self.cs or self.cs[k]- v < 0: # Break comparison if a source has number of expected symbol types less than a target 
             return False
        return True