1, Title: Given a string, the output of all dictionary order.
Such as:
Input string: ' AC ', Output: [' AC ', ' CA ']
Input string: ' abc ', Output: [' abc ', ' ACB ', ' bac ', ' BCA ', ' cab ', ' CBA ']
Input string: ' ACC ', output: [' acc ', ' CAC ', ' CCA ']
2. Recursion:
such as: ' abc ', for ' a ', return ' BC ' of the full permutation dictionary order, for ' B ', return ' AC ' of the full array, for ' C ', return ' AB ' the full array. "Loop plus recursion"
Code Listing 1:
defPrintstr (s): Liststr=[] Result=[] ifLen (s) ==1: Liststr.append (s)returnListstrElse: forIinchRange (len (s)): Liststr1=printstr (s[:i]+s[i+1:]) forIteminchliststr1:result.append (S[i]+str (item))returnlist (set (result)) s=input ()Print(Printstr (s))
Code Listing 2:
res =list ()defTraverse (ss,join_ss="'): ifSS: forI,sinchEnumerate (ss): Sub_ss= Ss[:i]+ss[i+1:] Traverse (SUB_SS,JOIN_SS+s)elifJoin_ss andJoin_ss not inchres:res.append (JOIN_SS)returnResresult= Traverse ('ABC',"')Print(Result)
Algorithm 9-----Output full Array (recursive)