[英]How can I make the program to output the string which both two strings end on in Python?
我有兩個字符串,例如
str1 = "Come"
str2 = "Rome"
我希望程序到 output ome。
我怎樣才能做到這一點?
這是我嘗試過的:
def getString(x):
return x
def solve(s1, s2):
a = getString(s1[0])
b = getString(s2[0])
for i in range(1, len(s1)):
if s1[i] != s1[i - 1]:
a += getString(s1[i])
for i in range(1, len(s2)):
if s2[i] != s2[i - 1]:
b += getString(s2[i])
if a == b:
print(a)
return True
return False
編輯:給出了錯誤的答案。 這個答案有效,但不是最有效的,雖然很簡單
for i in range(len(a)):
if b.endswith(a[i:]):
print(a[i:])
return
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.