How to check if second string is subsequence of first string in Python

1 Answer

0 votes
def is_second_string_subsequence_of_first_string(first, second):
    i = 0
    j = 0
    while i < len(first):
        if j >= len(second):
            return False
        if first[i] == second[j]:
            print("first[i] =", first[i], "second[j] =", second[j], "i =", i, "j =", j)
            i += 1
        j += 1
    return True

first = "python programming"
second = "python pro programming"

print("yes" if is_second_string_subsequence_of_first_string(first, second) else "no")



'''
run:

first[i] = p second[j] = p i = 0 j = 0
first[i] = y second[j] = y i = 1 j = 1
first[i] = t second[j] = t i = 2 j = 2
first[i] = h second[j] = h i = 3 j = 3
first[i] = o second[j] = o i = 4 j = 4
first[i] = n second[j] = n i = 5 j = 5
first[i] =   second[j] =   i = 6 j = 6
first[i] = p second[j] = p i = 7 j = 7
first[i] = r second[j] = r i = 8 j = 8
first[i] = o second[j] = o i = 9 j = 9
first[i] = g second[j] = g i = 10 j = 14
first[i] = r second[j] = r i = 11 j = 15
first[i] = a second[j] = a i = 12 j = 16
first[i] = m second[j] = m i = 13 j = 17
first[i] = m second[j] = m i = 14 j = 18
first[i] = i second[j] = i i = 15 j = 19
first[i] = n second[j] = n i = 16 j = 20
first[i] = g second[j] = g i = 17 j = 21
yes

'''

 



answered Mar 24, 2024 by avibootz

Related questions

...