如何测试,如果一个字符串是另一个子? [英] How to test if one string is a subsequence of another?
问题描述
如何测试如果一个字符串是另一个子?
这是一个较弱的条件比当子。例如,伊朗是不是爱尔兰的子字符串,但它是一个序列爱尔兰
。所不同的是一个子序列不必是连续的。
更多的例子:
- 在印尼'包含'印度'。
印度尼西亚
- 在罗马尼亚包含阿曼。
罗马尼亚
- 在马拉维包含马里。
马拉维
Movitation:我的朋友喜欢文字游戏。昨天我们打了'国家的国家内。我很好奇,如果有我们错过任何对。
编辑:如果您不熟悉子的数学定义
一个子序列是可以从另一序列删去一些元素导出,而不改变其余元素的顺序的序列
我做了
高清is_subsequence(X,Y):
测试x是否是y一子
X =列表(x)
对于字母Y:
如果X和X [0] ==信:
x.pop(0)
返回不是X
How to test if one string is a subsequence of another?
This is a weaker condition than being a substring. For example 'iran' is not a substring of 'ireland', but it is a subsequence IRelANd
. The difference is a subsequence doesn't have to be contiguous.
More examples:
- 'indonesia' contains 'india'.
INDonesIA
- 'romania' contains 'oman'.
rOMANia
- 'malawi' contains 'mali'.
MALawI
Movitation: My friends like word games. Yesterday we played 'countries within countries'. I am curious if there are any pairs we missed.
Edit: If you aren't familiar with the mathematical definition of subsequence
A subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements
I did
def is_subsequence(x, y):
"""Test whether x is a subsequence of y"""
x = list(x)
for letter in y:
if x and x[0] == letter:
x.pop(0)
return not x
这篇关于如何测试,如果一个字符串是另一个子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!