forked from Sandip-Jana/Algorithms-ACM-ICPC
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIterativePermutatios.java
More file actions
32 lines (32 loc) · 1.11 KB
/
IterativePermutatios.java
File metadata and controls
32 lines (32 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.*;
import java.lang.*;
import java.io.*;
class IterativePermutations
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
printPermutationsIterative("aabbc");
}
private static void printPermutationsIterative(String string){
int [] factorials = new int[string.length()+1];
factorials[0] = 1;
for (int i = 1; i<=string.length();i++) {
factorials[i] = factorials[i-1] * i;
}
int cnt=0;
for (int i = 0; i < factorials[string.length()]; i++) {
String onePermutation="";
String temp = string;
int positionCode = i;
for (int position = string.length(); position > 0 ;position--){
int selected = positionCode / factorials[position-1];
onePermutation += temp.charAt(selected);
positionCode = positionCode % factorials[position-1];
temp = temp.substring(0,selected) + temp.substring(selected+1);
}
cnt+=1;
System.out.println(cnt+" "+onePermutation);
}
}
}