title: Generate permutations of strings with lengths 2 to N given N letters in Java
body: <p>Given the following recursive Java method:</p>
  
  <pre><code>protected static void makeStringsList(List&lt;String&gt; strings, List&lt;Character&gt; letters) {
    if (letters.size() == 2) {
      strings.add(letters.get(0) + "" + letters.get(1));
      strings.add(letters.get(1) + "" + letters.get(0));
    }
    else {
      Character c = letters.remove(0);
      makeStringsList(strings, letters);
      List&lt;String&gt; tempList = new ArrayList&lt;String&gt;();
      for (String s : strings) {
        StringBuffer buffer = new StringBuffer(s);
        for (int index = 0; index &lt; s.length() + 1; index++) {
          buffer.insert(index, c);
          tempList.add(buffer.toString());
          buffer = new StringBuffer(s);
        }
      }
      strings.addAll(tempList);
    }
  }
  </code></pre>
  
  <p>Given N letters, the code above generates a list that contains permutations of strings that use all of those same N letters. For example, given (1, 2, 3), it generates:<ul><li>123</li><li>132</li><li>213</li><li>231</li><li>312</li><li>321</li></ul>It also adds strings with fewer than N letters to the list, but they're not exhaustive. Using the same example, 23 and 32 are also in the list, but 12, 21, 13, and 31 are not in the list.</p>
  
  <p>I had originally created the method above for a kata I was previously working on <a href="http://www.thetravelingcs.com/2011/09/java-kata-following-orders.html" rel="nofollow" title="here">here</a> during my free time, and I want to modify it now so that I can make it more general purpose and return a list containing permutations of strings <strong>with lengths 2 to N</strong> given N letters. Is it possible to modify the method above to accomplish this task? Any tips? Your advice will be greatly appreciated!</p>