我想编写一个函数来从两个字符串A,B中返回最短的字符串C,并确保字符串A,B是C的子字符串.但是键的长度A不必长于B ex:
A:'abcd',B:'cde'=> C:'abcde'#c,d是重复的
A:'abcd',B:'ecd'=> C:'abcdecd'#no字符重复,所以C是A + B
A:'abc',B:'cdeab'=> C:'cdeabc'A:'bce'
,B:'eabc'=> C:'eabce'#eabcd的长度为5,bceabc的长度为6
答:'',B:'abc'=> C:'abc'
答:'abc',B:''=> C:'abc'
我有以下功能,但似乎不正确
def checksubstring(A, B):
if not A or not B: return A if not B else B
index, string = 0, ''
for i, c in enumerate(A):
index = index + 1 if c == B[index] else 0
string += c
return string + B[index:]
Run Code Online (Sandbox Code Playgroud) 对于我来说,从字符串创建字典的最佳方法是什么,键是每个字符,其大写和值是相反的情况?我可以使用两行词典全面但更好的方法吗?例如:
string:abc => {'a':'A','b':'B','c':'C','C':'c','B':'b','A': '一个'}
string = 'abc'
d = { i:i.upper() for i in string}
d.update({ i.upper():i for i in string})
Run Code Online (Sandbox Code Playgroud)