title: Smart way to generate permutation and combination of String
body: <pre><code>String database[] = {'a', 'b', 'c'};
  </code></pre>
  
  <p>I would like to generate the following strings sequence, based on given <code>database</code>.</p>
  
  <pre><code>a
  b
  c
  aa
  ab
  ac
  ba
  bb
  bc
  ca
  cb
  cc
  aaa
  ...
  </code></pre>
  
  <p>I can only think of a pretty "dummy" solution.</p>
  
  <pre><code>public class JavaApplication21 {
  
      /**
       * @param args the command line arguments
       */
      public static void main(String[] args) {
          char[] database = {'a', 'b', 'c'};
  
          String query = "a";
          StringBuilder query_sb = new StringBuilder(query);
          for (int a = 0; a &lt; database.length; a++) {
              query_sb.setCharAt(0, database[a]);
              query = query_sb.toString();                    
              System.out.println(query);            
          }
  
          query = "aa";
          query_sb = new StringBuilder(query);
          for (int a = 0; a &lt; database.length; a++) {
              query_sb.setCharAt(0, database[a]);    
              for (int b = 0; b &lt; database.length; b++) {    
                  query_sb.setCharAt(1, database[b]);    
                  query = query_sb.toString();                    
                  System.out.println(query);
              }
          }
  
          query = "aaa";
          query_sb = new StringBuilder(query);
          for (int a = 0; a &lt; database.length; a++) {
              query_sb.setCharAt(0, database[a]);    
              for (int b = 0; b &lt; database.length; b++) {    
                  query_sb.setCharAt(1, database[b]);    
                  for (int c = 0; c &lt; database.length; c++) {                    
                      query_sb.setCharAt(2, database[c]);                        
                      query = query_sb.toString();                    
                      System.out.println(query);
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>The solution is pretty dumb. It is not scale-able in the sense that</p>
  
  <ol>
  <li>What if I increase the size of <code>database</code>?</li>
  <li>What if my final targeted print String length need to be N?</li>
  </ol>
  
  <p>Is there any smart code, which can generate scale-able permutation and combination string in a really smart way?</p>