import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.stream.*;
import java.util.stream.Collectors; 

class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		String a = "ABC"; 

        StringBuilder b = new StringBuilder(); 
        
        List<String> combinations = new ArrayList<>();
        
        combine(a, b, 0, 0, combinations);
        
        List<String> filtered = combinations.stream()
        
                                                  .filter(t -> t.length() == 2)
                                                  
                                                  .collect(Collectors.toList());

        
        filtered.stream()
        
                .forEach(t -> {
                	
                	        List<String> permutations = new ArrayList<>();
                	        
                	        StringBuilder sb = new StringBuilder();
                	        
					        boolean used[] = new boolean[2]; 
					        
					        used[0] = false; 
					        
					        used[1] = false; 
					        
					        permute(t, sb, used, permutations); 
					        
					        for(String s: permutations){ 
					        	
					            System.out.println(s); 
					            
					        } 
					        
                });
                
	}
	
	public static void combine(String a, StringBuilder b, int start, int level, List<String> combinations) { 

     for (int i = start; i < a.length(); i++) { 
     	
        if (b.toString().contains(String.valueOf(a.charAt(i))) == false) { 
        	
            b.append(a.charAt(i)); 
            
            combinations.add(b.toString()); 
            
            if (i < a.length() - 1) { 
            	
                combine(a, b, start + 1, level + 1, combinations); 
                
            } 
            
            b.setLength(b.length() - 1); 
            
        } 
        
     } 

  } 
  
    public static void permute(String a, StringBuilder b, boolean[] used, List<String> permutations) { 

        if ( b.length() == a.length()) { 
        	
            permutations.add(b.toString()); 
            
            return; 
        } 

        for (int i  =  0; i < a.length(); i++) { 

            if (used[i]) {continue;} 

            used[i] = true; 

            b.append(a.charAt(i)); 

            permute(a, b, used, permutations); 

            b.setLength(b.length()-1); 

            used[i] = false; 

        }  

    } 

} 
