1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| """ 思路
in_put = 'abc' n = 3個字元 組合數量 = n^n = 3^3 = 27 Big O: O(n^n)
""" def virtual_code(): answer = [] for v1 in 'abc': for v2 in 'abc': for v3 in 'abc': print(v1+v2+v3) answer.append(v1+v2+v3) """ foreach(n)+foreach(n)+foreach(n) a - a - a =>aaa - b =>aab - c =>aac a - b - a =>aba - b =>abb - c =>abc a - c - a =>aca - b =>acb - c =>acc b - a - a =>baa - b =>bab - c =>bac b - b - a =>bba - b =>bbb - c =>bbc b - c - a =>bca - b =>bcb - c =>bcc c - a - a =>caa - b =>cab - c =>cac c - b - a =>cba - b =>cbb - c =>cbc c - c - a =>cca - b =>ccb - c =>ccc """
""" 遞迴過程: combi(in_put='abc', next_size=3, prefix='') combi(in_put='abc', next_size=2, prefix='a') combi(in_put='abc', next_size=1, prefix='aa') combi(in_put='abc', next_size=0, prefix='aaa') =>answer.append(prefix) combi(in_put='abc', next_size=0, prefix='aab') =>answer.append(prefix) combi(in_put='abc', next_size=0, prefix='aac') =>answer.append(prefix) combi(in_put='abc', next_size=1, prefix='ab') combi(in_put='abc', next_size=0, prefix='aba') =>answer.append(prefix) combi(in_put='abc', next_size=0, prefix='abb') =>answer.append(prefix) combi(in_put='abc', next_size=0, prefix='abc') =>answer.append(prefix) combi(in_put='abc', next_size=1, prefix='ac') combi(in_put='abc', next_size=0, prefix='aca') =>answer.append(prefix) combi(in_put='abc', next_size=0, prefix='acb') =>answer.append(prefix) combi(in_put='abc', next_size=0, prefix='acc') =>answer.append(prefix) combi(in_put='abc', next_size=2, prefix='b') combi(in_put='abc', next_size=1, prefix='ba') combi(in_put='abc', next_size=0, prefix='baa') =>answer.append(prefix) combi(in_put='abc', next_size=0, prefix='bab') =>answer.append(prefix) combi(in_put='abc', next_size=0, prefix='bac') =>answer.append(prefix) combi(in_put='abc', next_size=1, prefix='bb') combi(in_put='abc', next_size=0, prefix='bba') =>answer.append(prefix) combi(in_put='abc', next_size=0, prefix='bbb') =>answer.append(prefix) combi(in_put='abc', next_size=0, prefix='bbc') =>answer.append(prefix) combi(in_put='abc', next_size=1, prefix='bc') combi(in_put='abc', next_size=0, prefix='bca') =>answer.append(prefix) combi(in_put='abc', next_size=0, prefix='bcb') =>answer.append(prefix) combi(in_put='abc', next_size=0, prefix='bcc') =>answer.append(prefix) combi(in_put='abc', next_size=2, prefix='c') combi(in_put='abc', next_size=1, prefix='ca') combi(in_put='abc', next_size=0, prefix='caa') =>answer.append(prefix) combi(in_put='abc', next_size=0, prefix='cab') =>answer.append(prefix) combi(in_put='abc', next_size=0, prefix='cac') =>answer.append(prefix) combi(in_put='abc', next_size=1, prefix='cb') combi(in_put='abc', next_size=0, prefix='cba') =>answer.append(prefix) combi(in_put='abc', next_size=0, prefix='cbb') =>answer.append(prefix) combi(in_put='abc', next_size=0, prefix='cbc') =>answer.append(prefix) combi(in_put='abc', next_size=1, prefix='cc') combi(in_put='abc', next_size=0, prefix='cca') =>answer.append(prefix) combi(in_put='abc', next_size=0, prefix='ccb') =>answer.append(prefix) combi(in_put='abc', next_size=0, prefix='ccc') =>answer.append(prefix)
""" def combi(in_put, next_size, prefix=""): if next_size == 0: answer.append(prefix) return
for i in range(0, len(in_put)): combi(in_put=in_put, next_size=next_size - 1, prefix=f'{prefix}{in_put[i]}', )
answer = [] in_put = "abc" combi(in_put, len(in_put)) print("\n".join(answer))
|