Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Of all the solutions given here and in other forums, I liked Mark Byers the most. That description actually made me think and code it myself. Too bad I cannot voteup his solution as I am newbie.<br> Anyways here is my implementation of his description</p> <pre><code>public class PermTest { public static void main(String[] args) throws Exception { String str = "abcdef"; StringBuffer strBuf = new StringBuffer(str); doPerm(strBuf,str.length()); } private static void doPerm(StringBuffer str, int index){ if(index &lt;= 0) System.out.println(str); else { //recursively solve this by placing all other chars at current first pos doPerm(str, index-1); int currPos = str.length()-index; for (int i = currPos+1; i &lt; str.length(); i++) {//start swapping all other chars with current first char swap(str,currPos, i); doPerm(str, index-1); swap(str,i, currPos);//restore back my string buffer } } } private static void swap(StringBuffer str, int pos1, int pos2){ char t1 = str.charAt(pos1); str.setCharAt(pos1, str.charAt(pos2)); str.setCharAt(pos2, t1); } } </code></pre> <p>I prefer this solution ahead of the first one in this thread because this solution uses StringBuffer. I wouldn't say my solution doesn't create any temporary string (it actually does in <code>system.out.println</code> where the <code>toString()</code> of StringBuffer is called). But I just feel this is better than the first solution where too many string literals are created. May be some performance guy out there can evalute this in terms of 'memory' (for 'time' it already lags due to that extra 'swap')</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload