Advertisement
Guest User

Combinations in a String

a guest
Aug 24th, 2011
556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.68 KB | None | 0 0
  1. public class StringLib {
  2.        
  3.     public static void combine(String str){
  4.         int length=str.length();
  5.        
  6.         StringBuffer output=new StringBuffer(length);
  7.        
  8.         combination(str,length,output,0);
  9.     }
  10.    
  11.     static void combination(String str, int length, StringBuffer output, int src){
  12.         /* show arms-length recursion style with better peformance */
  13.         if (src==length)
  14.             return;
  15.         else{
  16.             for(int i=src;i<length;i++){
  17.                 output.append(str.charAt(i));
  18.                 System.out.println(output.toString());
  19.                 //if (i<length-1)
  20.                 combination(str,length,output,i+1);
  21.                 output.deleteCharAt(output.length()-1);
  22.             }
  23.         }
  24.     }
  25.    
  26.        
  27.     public static void main(String[] args){
  28.         combine("abc");
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement