Full permutation problem - iteration Interpolation Method

Subject description:

  Please prepare a method for determining all permutations of a string of a given string, the string returned all permutations

   For example: "ABC" as a result of the arrangement of the whole: ABC, ACB, BAC, BCA, CAB, CBA

  (Meaning of the questions: is that all of the results for each character of a string rearranging out)

Problem-solving methods: iterative Interpolation Method

  Principle : in order to take each character of the string, and the results of the last inserted empty, when the last character to take the time to return the result.

  Realization of ideas:

   Note : Because the output is a result of the arrangement, an array of words can not determine the length of the array, consider the results of a set of type String to store output .

   The first step : create a collection that used to hold all the results arranged.

   Step Two : initialize the collection, give a first character set to add str for iterative back Interpolation

      The third step : The second character str new character, are inserted into air taken in a set of previously generated.

   Step four : re-create a new collection, used to hold a new character into different positions all results

   Step Five : Find new character

       Step Six : a collection of the visit, and are inserted from a different location of the new collection.

  Step Seven : Update list after insertion is completed. And return.

  Specific code:

    // iteration Interpolation Method Code: 
    public  static the ArrayList <String> Permutation (STR String, int len) {   // STR: cur string to be arranged: a recursive state 
 // Step 1: Create a set, used to hold all order results after   
        the ArrayList <String> List = new new the ArrayList <> ();
 // Step: initialize the collection, str added to the first character set, for insertion of empty later iteration process (otherwise, hyphens no, where to insert blank)? 
        list.add (str.charAt (0) + "");           // Since str.charAt (0) is a character, it must be converted to a string
 // third step: the first str 2 characters for the new characters are inserted into the space previously generated each element of the collection 
        for ( int I =. 1; I <len; I ++) {                                          // Note: I is the index of str
 @ fourth step: re create a new collection, used to hold a new character into a different position results
            ArrayList <String> = list_new new new ArrayList <> ();
 // Step five: Get new character 
            char c = str.charAt (i);
 // Step Six: a collection of the visit, and each new collection inserted from different locations 
            for (S String: List) { 
                String newStr = S + C;     // new character is inserted on the left, and added to a new collection 
                list_new.add (newStr); 
                newStr = S + C;         // new character is inserted in the right side, and added to a new collection 
                list_new.add (newStr);
                 // inserted in the middle    
                for ( int J =. 1; J <s.length (); J ++) { // first intermediate from s empty begin inserting
                    = s.substring newStr (0, J) + C + s.substring (J); 
                    list_new.add (newStr); 
                } 
            } 
// Step Seven: after completely inserted, updated List 
            List = list_new; 
        } 
        return List; 
    }

 

Guess you like

Origin www.cnblogs.com/songchengyu/p/12593932.html