Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- Welcome to GDB Online.
- GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
- C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
- Code, Compile, Run and Debug online from anywhere in world.
- *******************************************************************************/
- import java.util.*;
- public class Main
- {
- public static void main (String[]args)
- {
- List < String > list = singlePermutations ("aabb");
- for (String s:list)
- {
- System.out.println (s);
- }
- }
- public static List < String > singlePermutations (String s)
- {
- // Your code here!
- ArrayList < String > allPermutations = new ArrayList <> ();
- String[]letters = s.split ("");
- int permutationCount = 1;
- int length = s.length ();
- ArrayList < Integer > pList = new ArrayList <> ();
- pList.add (permutationCount);
- for (int i = 1; i <= s.length (); i++)
- {
- permutationCount *= i;
- pList.add (permutationCount);
- }
- String[][]permutations = new String[permutationCount][s.length ()];
- int amount=0;
- int repetitions=0;
- int totalRepetitions=0;
- int startRow=0;
- int column=0;
- try {
- for (int i = 0; i < length; i++){
- amount = pList.get(pList.size()-2-i);
- repetitions = pList.get(pList.size()-2)/amount;
- totalRepetitions = repetitions*length;
- startRow = 0;
- column = i;
- //System.out.println(String.format("Amount: %d - Repititions: %d - Total Repititions: %d", amount, repetitions, totalRepetitions));
- for (int j = 0; j < totalRepetitions; j++){
- for (int k = startRow; k < startRow+amount; k++){
- while (permutations[k][column%length] != null){
- column++;
- }
- permutations[k][column%length] = letters[i];
- }
- column++;
- startRow+=amount;
- }
- }
- } catch (Exception e) {
- System.out.println(String.format("Amount: %d - Repititions: %d - Total Repititions: %d - Start Row: %d - Column: %d", amount, repetitions, totalRepetitions, startRow, column));
- }
- StringBuilder sb;
- for (int i = 0; i < permutationCount; i++)
- {
- sb = new StringBuilder ("");
- for (int j = 0; j < length; j++)
- {
- sb.append (permutations[i][j]);
- }
- if(!allPermutations.contains(sb.toString ())){
- allPermutations.add (sb.toString ());
- }
- }
- boolean test = true;
- if (test) return allPermutations;
- ArrayList < String > singlePermutations = new ArrayList <> ();
- while (!allPermutations.isEmpty ())
- {
- singlePermutations.add (allPermutations.get (0));
- allPermutations.removeAll (singlePermutations);
- }
- return singlePermutations;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement