manthanthakker40

Permutations

Jan 13th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.88 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package code;
  7.  
  8. import java.util.ArrayList;
  9. import java.util.Arrays;
  10. import java.util.LinkedHashMap;
  11. import java.util.LinkedList;
  12. import java.util.List;
  13. import java.util.Scanner;
  14.  
  15. /**
  16.  *
  17.  * @author manth
  18.  */
  19. public class Code {
  20.  
  21.     /**
  22.      * @param args the command line arguments
  23.      */
  24.     /*
  25.     Given a string s and a non-empty string p, find all the start indices of p's anagrams in s.
  26.  
  27. Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100.
  28.  
  29. The order of output does not matter.
  30.  
  31. Example 1:
  32.  
  33. Input:
  34. s: "cbaebabacd" p: "abc"
  35.  
  36. Output:
  37. [0, 6]
  38.  
  39. Explanation:
  40. The substring with start index = 0 is "cba", which is an anagram of "abc".
  41. The substring with start index = 6 is "bac", which is an anagram of "abc".
  42.     */
  43.     public static void main(String[] args) {
  44.        Scanner sc=new Scanner(System.in);
  45.        String s=sc.nextLine();
  46.        
  47.        
  48.        permutations(0,"abc");
  49.      
  50.        
  51.        
  52.      
  53.        
  54.    
  55.      
  56.        }
  57.     public static void permutations(int depth,String input)
  58.     {
  59.         if(depth==input.length())
  60.         {
  61.             System.out.println(input);
  62.             return;
  63.         }
  64.        // char input_arr[]=input.toCharArray();
  65.         for(int i=depth;i<input.length();i++)
  66.         {
  67.             char input_arr[]=input.toCharArray();
  68.             char tmp=input_arr[depth];
  69.             input_arr[depth]=input_arr[i];
  70.             input_arr[i]=tmp;
  71.            
  72.             String swaped=new String(input_arr);
  73.             permutations(depth+1,swaped);
  74.         }
  75.        
  76.     }
  77.    
  78.    
  79.    
  80.    
  81.    
  82.        
  83.        
  84.    
  85.    
  86.    
  87. }
Add Comment
Please, Sign In to add comment