-
Notifications
You must be signed in to change notification settings - Fork 94
/
Building a List.py
52 lines (39 loc) · 1.34 KB
/
Building a List.py
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
"""
Problem Statement
Chan has decided to make a list of all possible combinations of letters of a given string S. If there are two strings
with the same set of characters, print the lexicographically smallest arrangement of the two strings.
abc acb cab bac bca
all the above strings' lexicographically smallest string is abc.
Each character in the string S is unique. Your task is to print the entire list of Chan's in lexicographic order.
for string abc, the list in lexicographic order is given below
a ab abc ac b bc c
"""
__author__ = 'Danyang'
class Solution(object):
def solve(self, cipher):
"""
main solution function
:param cipher: the cipher
"""
s = cipher
s = "".join(sorted(list(s)))
result = []
self.dfs(s, "", result)
return "\n".join(result[1:])
def dfs(self, seq, cur, result):
result.append(cur)
if seq:
for i in xrange(len(seq)):
self.dfs(seq[i + 1:], cur + seq[i], result)
if __name__ == "__main__":
import sys
f = open("1.in", "r")
# f = sys.stdin
testcases = int(f.readline().strip())
for t in xrange(testcases):
# construct cipher
N = int(f.readline().strip())
cipher = f.readline().strip()
# solve
s = "%s\n" % (Solution().solve(cipher))
print s,