Results 1 to 3 of 3
  1. #1
    marchello is offline Member
    Join Date
    Apr 2013
    Posts
    15
    Rep Power
    0

    Question permutations - code efficiency

    Hi all,
    I'm on my learning task to find all possible permutations. It works fine for short strings, but tooks too long for longer strings. Basically, my approach is to randomize string, add results to the list and then get rid of duplicates in the list. Please help me to improve code efficiency. Just to clarify, finally I should return middle result for even length of input string or length/2 for odd one (this part is ok, just to let you know what my code does).
    Thanks ahead.

    Java Code:
    package learn;
    
    import java.util.List;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.HashSet;
    import java.util.Set;
    
    public class simpleFun159MiddlePermutation {
    
        public static String findMidPerm(String strng) {
            // your code here!
            String result = "";
            List<String> mylist = new ArrayList<>();
            
            int i = 0;
    		while (true){
        		List<Character> characters = new ArrayList<Character>();
                for(char c:strng.toCharArray()){
                    characters.add(c);
                }
                StringBuilder output = new StringBuilder(strng.length());
                while(characters.size()!=0){
                    int randPicker = (int)(Math.random()*characters.size());
                    output.append(characters.remove(randPicker));
                }
                //System.out.println(output.toString());
                mylist.add(output.toString());
                i = i + 1;
                if (i>50000000){break;}
    		}
        	Set<String> hs = new HashSet<>();
        	hs.addAll(mylist);
        	mylist.clear();
        	mylist.addAll(hs);
        	Collections.sort(mylist);
    		//System.out.println(mylist);
    		//System.out.println(mylist.size());
    		int myindex = 0;
    		if ( (myindex & 1) == 0 ) { myindex = (mylist.size() / 2)-1; } else { myindex = (mylist.size() / 2); }
    		//System.out.println(mylist.get(myindex));
    		result = mylist.get(myindex);
        	return result;
        }
    
        public static void main(String[] args) {
        	String s = "abc";
        	//String s = "wulaydcrjhzt"; 
        	System.out.println(findMidPerm(s));
      	}
    }
    Last edited by marchello; 10-01-2018 at 09:11 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: permutations - code efficiency

    Search this forum for 'permutation'. we're babbling a lot about it as well as` 'combinations'.

    kind regards,

    Jos
    Last edited by JosAH; 10-05-2018 at 05:02 PM.
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    marchello is offline Member
    Join Date
    Apr 2013
    Posts
    15
    Rep Power
    0

    Default Re: permutations - code efficiency

    Hi Jos,
    Thanks for your reply.

Similar Threads

  1. help with permutations of a string
    By hoosierfan24 in forum New To Java
    Replies: 8
    Last Post: 05-25-2011, 06:35 PM
  2. Efficiency of code...
    By Inventor22 in forum New To Java
    Replies: 6
    Last Post: 09-26-2010, 10:24 AM
  3. array permutations
    By mtK in forum Advanced Java
    Replies: 4
    Last Post: 04-08-2010, 05:57 AM
  4. Permutations
    By m00nchile in forum New To Java
    Replies: 3
    Last Post: 03-25-2010, 08:58 PM
  5. Find all permutations of a number
    By matzahboy in forum New To Java
    Replies: 6
    Last Post: 12-02-2008, 03:59 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •