Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting every possible permutation of a string or combination including repeated characters in Java
    text
    copied!<p>I have been trying to generate a list of every possible 4 character string which could be made up of any given set of characters. I have used a function to generate every 4 character combination from a set of characters but each character is only ever used once. I need every possible combination using a given set of chars for example:</p> <pre><code>String[] elements = {"a", "b", "c", "1", "2", "3"}; int[] indices; CombinationGenerator x = new CombinationGenerator (elements.length, 4); StringBuffer combination; while (x.hasMore ()) { combination = new StringBuffer (); indices = x.getNext (); for (int i = 0; i &lt; indices.length; i++) { combination.append (elements[indices[i]]); } System.out.println (combination.toString ()); } </code></pre> <p>Using the CombinationGenerator class from <a href="http://www.ccs.neu.edu/home/lieber/courses/csu670/f08/sdg-players/winning-player/src/logic/CombinationGenerator.java" rel="nofollow noreferrer">here</a>, this will return every unique 4 character combination such as:</p> <pre><code>'abcd' , 'abc1', 'acb2', 'acb1' </code></pre> <p>But, I want every possible string that could be created using the given characters. For example:</p> <pre><code>'aaaa', 'aaab', 'abc1', 'aac1', '11c2' </code></pre> <p>I have tried every recursive and permutation method I've been able to find or come up with but I'm stumped on getting any further than generating all the combinations like above, then generating every permutation of each combination, but I can't work out how to create a set of combinations using repeated characters.</p> <p>Any help, or even just the theory on how it could be done would be helpful.</p>
 

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