Home > Enterprise >  In Python, how to know whether a string contain a jump substring. like 'abcd' contain 
In Python, how to know whether a string contain a jump substring. like 'abcd' contain 

Time:09-18

In Python, how to know whether a string contain a jump substring. like 'abcd' contain 'ad' 'I very like play basketball' contain 'like basketball'

CodePudding user response:

You can create an iterator from the test string and validate that every character in the subsequence can be found in the sequence of characters generated by the iterator:

def is_subsequence(a, b):
    seq = iter(b)
    return all(i in seq for i in a)

so that:

print(is_subsequence('ad', 'abcd'))
print(is_subsequence('adc', 'abcd'))

outputs:

True
False

Demo: https://ideone.com/jGdpis

CodePudding user response:

You can use regexes for that:

the regex r'a.{0,}d' will match abcd. Similarly, r'like.{0,}basketball' will match I very like play basketball.

Edit: Example 1

  • Related