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 婚前女儿生日祝福语简短