循环单词定义:
str1:picture
str2 : turepic
str1 与str2 是循环单词
代码:
def func(strs):
count = 0
flags = [False for _ in range(len(strs))]
for i in range(len(strs)):
for j in range(i+1, len(strs)):
if flags[j] == True:
continue
c = strs[i][0] #"p"
p1 = 0 #"index"
if strs[j].find(c) != -1:
p2_start = strs[j].index(c)
p2 = p2_start
while strs[i][p1] == strs[j][p2]:
p1 = (p1 + 1) % len(strs[i])
p2 = (p2 + 1) % len(strs[j])
if p1 == 0 and p2 == p2_start:
if not flags[i] and not flags[j]:
count += 1
flags[i] = True
flags[j] = True
break
return count
if __name__ == "__main__":
str1 = "picture"
str2 = "turepic"
str5 = "icturep"
str6 = "picture"
str7 = "picture"
str3 = "word"
str4 = "rdwo"
str8 = "aaa"
str9 = "aaa"
strs = [str1, str2, str3, str4, str5, str6, str7, str8, str9]
res = func(strs)
print(res)
给自己立一个flag, 每道算法题都要在20分钟写出bug free的代码。我知道这非常有难度,但是还是因为不熟练啊,孰能生巧。
思路: 5分钟,代码: 10 分钟, 优化: 5分钟。以后每次都要这样训练。