Advertisement
YChalk

Permutations

Feb 17th, 2022 (edited)
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.03 KB | None | 0 0
  1. /******************************************************************************
  2.  
  3. Welcome to GDB Online.
  4. GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
  5. C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
  6. Code, Compile, Run and Debug online from anywhere in world.
  7.  
  8. *******************************************************************************/
  9. import java.util.*;
  10.  
  11. public class Main
  12. {
  13.   public static void main (String[]args)
  14.   {
  15.     List < String > list = singlePermutations ("aabb");
  16.     for (String s:list)
  17.       {
  18.     System.out.println (s);
  19.       }
  20.   }
  21.  
  22.   public static List < String > singlePermutations (String s)
  23.   {
  24.     // Your code here!
  25.     ArrayList < String > allPermutations = new ArrayList <> ();
  26.     String[]letters = s.split ("");
  27.  
  28.     int permutationCount = 1;
  29.     int length = s.length ();
  30.  
  31.     ArrayList < Integer > pList = new ArrayList <> ();
  32.     pList.add (permutationCount);
  33.     for (int i = 1; i <= s.length (); i++)
  34.       {
  35.     permutationCount *= i;
  36.     pList.add (permutationCount);
  37.       }
  38.      
  39.     String[][]permutations = new String[permutationCount][s.length ()];
  40.     int amount=0;
  41.     int repetitions=0;
  42.     int totalRepetitions=0;
  43.     int startRow=0;
  44.     int column=0;
  45.     try {
  46.         for (int i = 0; i < length; i++){
  47.             amount = pList.get(pList.size()-2-i);
  48.             repetitions = pList.get(pList.size()-2)/amount;
  49.             totalRepetitions = repetitions*length;
  50.             startRow = 0;
  51.             column = i;
  52.             //System.out.println(String.format("Amount: %d - Repititions: %d - Total Repititions: %d", amount, repetitions, totalRepetitions));
  53.             for (int j = 0; j < totalRepetitions; j++){
  54.                 for (int k = startRow; k < startRow+amount; k++){
  55.                     while (permutations[k][column%length] != null){
  56.                         column++;
  57.                     }
  58.                     permutations[k][column%length] = letters[i];
  59.                 }
  60.                 column++;
  61.                 startRow+=amount;
  62.             }
  63.         }
  64.     } catch (Exception e) {
  65.         System.out.println(String.format("Amount: %d - Repititions: %d - Total Repititions: %d - Start Row: %d - Column: %d", amount, repetitions, totalRepetitions, startRow, column));
  66.     }
  67.    
  68.     StringBuilder sb;
  69.  
  70.     for (int i = 0; i < permutationCount; i++)
  71.       {
  72.     sb = new StringBuilder ("");
  73.     for (int j = 0; j < length; j++)
  74.       {
  75.         sb.append (permutations[i][j]);
  76.       }
  77.       if(!allPermutations.contains(sb.toString ())){
  78.         allPermutations.add (sb.toString ());
  79.       }
  80.       }
  81.      
  82.     boolean test = true;
  83.     if (test) return allPermutations;
  84.  
  85.     ArrayList < String > singlePermutations = new ArrayList <> ();
  86.  
  87.     while (!allPermutations.isEmpty ())
  88.       {
  89.     singlePermutations.add (allPermutations.get (0));
  90.     allPermutations.removeAll (singlePermutations);
  91.       }
  92.  
  93.     return singlePermutations;
  94.   }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement