So I have a string like this:
"abc"
I would need:
"abc"
"acb"
"bca"
"bac"
"cab"
"cba"
I tried:
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)
If works for string of size 3, but I believe it is very inefficient and also doesn't work for "abcd"
. Is there a better approach?
Update: I would like to solve by providing an algorithm. Actually currently working in a recursive way to solve it. Would prefer a solution that is not a python function solving the problem for me