如何检查一个字符串的所有字符是否依次出现在另一个字符串中?

时间:2019-12-28 09:59:27

标签: python python-3.x string

我想检查一个单词是否是另一个单词的有效缩写。为了有效,缩写词中的所有字母必须按顺序出现在原始单词中。

例如:

  • 'Msttum''Darmstadtium'的缩写
  • 'Amsi'不是'Samarium'的缩写

我该怎么做?

1 个答案:

答案 0 :(得分:2)

这是一个简单的解决方案:

def is_abbrev(word, abbrev):
    it = iter(word.lower())
    return all(x in it for x in abbrev.lower())

例如:

  • is_abbrev("Hello", "hell") => True
  • is_abbrev("Hello", "hLl") => True
  • is_abbrev("Hello", "llh") => False
相关问题