Python中检查两个句子是否相似的程序
假设我们有两个句子s和t。我们必须检查它们是否相似。这里的句子只有英文字母。当可以在这些给定句子之一中添加任意句子(可能为空)以使两个句子变得相等时,两个句子被称为相似。
因此,如果输入类似于s="weliveatcityKolkata"t="cityKolkata",那么输出将为True,因为我们可以通过添加句子“welivein”从t中获取s。
示例
让我们看看以下实现以获得更好的理解-
def solve(s, t):
s1 = s.split()
s2 = t.split()
if len(s1) > len(s2):
s1,s2 = s2,s1
while(s1):
if(s2[0]==s1[0]):
s2.pop(0)
s1.pop(0)
elif(s2[-1]==s1[-1]):
s2.pop()
s1.pop()
else:
return(False)
return(True)
s = "we live at city Kolkata"
t = "city Kolkata"
print(solve(s, t))输入
"we live at city Kolkata", "city Kolkata"输出结果
True
热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志