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 红包祝福语简短10字
18 周六早晨祝福语简短