Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 检查字符串是否为';你好';是另一个的子序列_Python_String_Loops_Methods_Find - Fatal编程技术网

Python 检查字符串是否为';你好';是另一个的子序列

Python 检查字符串是否为';你好';是另一个的子序列,python,string,loops,methods,find,Python,String,Loops,Methods,Find,例如,这是一个我们给它一个输入的问题 ahhellllloou 如果在示例中可以删除一些字母或忽略它们,最后我们到达hello,则必须print('YES'),否则打印('NO'),例如: input='ahhellllloou'output='YES'input='hlelo'output='NO'-因为在这个输入中我们无法找到hello单词,它是hlelo不是hello 另一个例子是'hezzrtlilo',我们可以删除zzrt和我,然后看到“hello”,在这种情况下,答案必须是真的,因为

例如,这是一个我们给它一个输入的问题

ahhellllloou

如果在示例中可以删除一些字母或忽略它们,最后我们到达
hello
,则必须
print('YES'),否则打印('NO')
,例如:

input='ahhellllloou'output='YES'input='hlelo'output='NO'
-因为在这个输入中我们无法找到hello单词,它是
hlelo
不是hello

另一个例子是
'hezzrtlilo'
,我们可以删除
zzrt
和我,然后看到“hello”,在这种情况下,答案必须是真的,因为最后我们看到hello


其他示例:
input='tymbzjyqhymedasloqbq'output='NO'
-因为在这个输入中,我们可以找到一个l而不是两个ll的helo,假设您正在检查一个字符串是否是另一个字符串的子序列

# From https://www.geeksforgeeks.org/given-two-strings-find-first-string-subsequence-second/
def isSubSequence(string1, string2, m = None, n = None): 
    m = len(string1) if m == None else m
    n = len(string2) if n == None else n
    # Base Cases 
    if m == 0:    return True
    if n == 0:    return False

    # If last characters of two strings are matching 
    if string1[m-1] == string2[n-1]: 
        return isSubSequence(string1, string2, m-1, n-1) 

    # If last characters are not matching 
    return isSubSequence(string1, string2, m, n-1) 
测试

tests = [ 'ahhellllloou' , 'hlelo']
for t in tests:
  response = "YES" if isSubSequence("hello", t) else "NO"
  print(f'Test Word {t} checks as {response}')
Test Word ahhellllloou checks as YES
Test Word hlelo checks as NO
输出

tests = [ 'ahhellllloou' , 'hlelo']
for t in tests:
  response = "YES" if isSubSequence("hello", t) else "NO"
  print(f'Test Word {t} checks as {response}')
Test Word ahhellllloou checks as YES
Test Word hlelo checks as NO

这里的问题到底是什么?你有什么特别的地方吗?这读起来有点像“这是一个家庭作业问题,有人发布了一个答案。”添加一些更清晰的内容和您所做的任何编码尝试都有助于缓解这一问题。请特别重复介绍教程。简单地转储一个未编辑的作业问题是不可接受的。@gatsu--您是在询问writer和checker,看一个单词是否是字符串的子序列。子序列是一个字符串,可以通过删除某些元素或不删除任何元素从字符串中派生出来?我正在检查像“hello”这样的字符串是否是另一个字符串的子序列,我不知道如何编写此代码,我们从用户处获取输入,并检查“hello”字符串是否是该字符串的子序列谢谢,这是我想要的,但我如何才能做到编写此代码时,输入的是我们捕捉到的用户输入,而不是我们之前给应用程序的输入