Algorithm 9-----Output full Array (recursive)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.