Hamza
Hamza

Reputation: 6055

Best way to check if a string contains consecutive substring?

I am trying to find the best way to check if a string has consecutive appearances of a particular substring. returning a boolean value e.g. only returning True if the whole string sits next to itself inside the string. Some example test cases:

consecutive_checker(string='blaablaa', substring = 'a')
>>> True

consecutive_checker(string='blaablaa', substring = 'aa')
>>> False

consecutive_checker(string='blaablaa', substring = 'blaa')
>>> True

consecutive_checker(string='blaablaa', substring = 'baa')
>>> False

My Apporach:

def consecutive_checker(string='blaablaa', substr = 'blaa'):
    count = string.count(substr)
    if count > 1:
        for sidx in range(len(string) - len(substr)+1):
            for ssidx in range(len(substr)+1):
                if string[sidx:sidx+ssidx] == string[sidx+ssidx:sidx+ssidx*2] == substr:
                    return True
    return False

Here for efficiency (If its not fairly obvious):

  1. It only proceeds if substring is appeared atleast twice.
  2. Lengthwise it cherrypicks indices
  3. Returns as soon as consecutive substrings are found

So I was thinking how I can improve it even further. Seems like builtin count() function although very efficient is iterating whole string. It could be a point of improvement but I cannot totally avoid this as I used it as my safety net to only iterate when needed. Similarly three ugly checks and dual for loops does not give off a efficiency vibe. So how can I improve it even more? Any suggestions are welcomed!

Upvotes: 1

Views: 811

Answers (2)

Barmar
Barmar

Reputation: 782584

You're really overcomplicating this. Just double the substring and check if that appears in the string.

def consecutive_checker(string, substr):
    return substr*2 in string

Upvotes: 3

etch_45
etch_45

Reputation: 792

Another approach is to use the .find() function which returns the first index of the argument (substring) passed. For the case when it does not find it, it will return -1.

>>> string = 'blaablaa'
>>> string.find('aa'*2)
-1
>>> string.find('blaa'*2)
0

Upvotes: 0

Related Questions