所以我有一个这样的字符串:
"abc"
Run Code Online (Sandbox Code Playgroud)
我会需要:
"abc"
"acb"
"bca"
"bac"
"cab"
"cba"
Run Code Online (Sandbox Code Playgroud)
我试过:
string = "abc"
combinations = []
for i in range(len(string)):
acc = string[i]
for ii in range(i+1,i+len(string)):
acc += string[ii%len(string)]
combinations.append(acc)
combinations.append(acc[::-1])
print(combinations)
Run Code Online (Sandbox Code Playgroud)
如果适用于大小为 3 的字符串,但我认为它非常低效并且也不适用于"abcd". 有没有更好的方法?
更新:我想通过提供算法来解决。实际上目前正在以递归方式解决它。更喜欢不是python函数的解决方案为我解决问题
python ×1