<p>如何测试一个字符串是否是另一个字符串的子序列?</p>
<p>这是一个弱于子串的条件。例如,“伊朗”不是“爱尔兰”的子串,但它是一个子串<code>IRelANd</code>。区别在于子序列不必是连续的。</p>
<p>更多示例:</p>
<ul>
<li>“印度尼西亚”包含“印度”。<code>INDonesIA</code></li>
<li>“罗马尼亚”包含“阿曼”。<code>rOMANia</code></li>
<li>“马拉维”包含“马里”。<code>MALawI</code></li>
</ul>
<p>我的朋友喜欢文字游戏。昨天我们玩了“国与国之间的游戏”。我想知道我们是否遗漏了几对。</p>
<p>编辑:如果你不熟悉子序列的数学定义</p>
<blockquote>
<p>A subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements</p>
</blockquote>
<p>是的</p>
<pre><code>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
</code></pre>