Files
tira/viikko8/anagrams.py
T
2023-11-07 01:27:43 +02:00

33 lines
683 B
Python

def create(s):
global not_used, result
not_used = []
result = []
not_used = [x for x in s]
haku(0, len(s), sorted(s))
print()
return sorted(result)
def haku(k, n, s):
print(s, not_used)
list = []
if k == n:
print(list)
result.append("".join(list))
else:
for i, x in enumerate(s):
if x not in not_used:
not_used.remove(x)
list.append(x)
haku(k + 1, n, s)
if __name__ == "__main__":
print(create("ab")) # [ab,ba]
print(create("abac"))
# [aabc,aacb,abac,abca,acab,acba,baac,baca,bcaa,caab,caba,cbaa]
print(len(create("aybabtu"))) # 1260