Home > front end >  Given two strings, find if 5 characters or more of the first string is a section of the second and v
Given two strings, find if 5 characters or more of the first string is a section of the second and v

Time:09-13

Example input:
str1 = 'ZZZABCDEFRFR'
str2 = 'DDDDDDDDDDDDABCDERR'
Example output: True
Example input:
str1 = 'DDDDDDDDDDDDABACDERR'
str2 = 'ZZZABCDEFRFR'
Example output: False

I tried something similar to this but it skips characters until it finds one matching. This is not what I want...

def issubsequence(s1, s2):
 
    n,m = len(s1),len(s2)
    i,j = 0,0
    while (i < n and j < m):
        if (s1[i] == s2[j]):
            i  = 1
        j  = 1
     
    # If i reaches end of s1,that mean we found all
    # characters of s1 in s2,
    # so s1 is subsequence of s2, else not
    return i == n

CodePudding user response:

Use a for loop to iterate through your first string. Then use the in operator to check if it is a substring in the second. The code below checks if str1 is in str2. You can swap them to check the other way.

for i in range(len(str1) - 4):
    substr1 = str1[i:i 5]
    if substr1 in str2:
        print(f'{substr1} in {str2}')

CodePudding user response:

You can use a set intersection on slices to the two strings:

def f(s1,s2,len_=5):
    if min(map(len, [s1,s2]))<len_: return False

    return bool({s1[i:i len_] for i in range(0,len(s1)-len_ 1)} & 
                {s2[i:i len_] for i in range(0,len(s2)-len_ 1)})

Test it:

cases=[(True,'ZZZABCDEFRFR','DDDDDDDDDDDDABCDERR'), 
        (False,'DDDDDDDDDDDDABACDERR','ZZZABCDEFRFR'),
        (True,'abcde','abcde'),
        (True,'xabcde','yabcde')]

for b,x,y in cases:
    print(b,x,y,f(x,y))

Prints:

True ZZZABCDEFRFR DDDDDDDDDDDDABCDERR True
False DDDDDDDDDDDDABACDERR ZZZABCDEFRFR False
True abcde abcde True
True xabcde yabcde True

Alternatively, use next with a generator with same substring slices:

def f2(s1,s2,len_=5):
    if min(map(len, [s1,s2]))<len_: return False
    return next((True for i in range(0,len(s1)-len_ 1) 
                   if s1[i:i len_] in s2), False) 
  • Related