-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutations
More file actions
62 lines (55 loc) · 1.39 KB
/
Copy pathpermutations
File metadata and controls
62 lines (55 loc) · 1.39 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
Given a string, print all permutations of a given string.
Input:
The first line of input contains an integer T, denoting the number of test cases.
Each test case contains a single string S in capital letter.
Output:
For each test case, print all permutations of a given string S with single space and all permutations should be in lexicographically increasing order.
Constraints:
1 ≤ T ≤ 10
1 ≤ size of string ≤ 5
Example:
Input:
2
ABC
ABSG
Output:
ABC ACB BAC BCA CAB CBA
ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA
*/
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t>0)
{
t--;
String s=sc.next();
char ch[]=new char[s.length()];
for(int i=0;i<ch.length;i++)
ch[i]=s.charAt(i);
Arrays.sort(ch);
s="";
for(int i=0;i<ch.length;i++)
s+=ch[i];
permutation("",s);
System.out.println();
}
}
public static void permutation(String prev,String s)
{
int n=s.length();
if(n==0)
System.out.print(prev+" ");
else
{
for(int i=0;i<n;i++)
permutation(prev+s.charAt(i),s.substring(0,i)+s.substring(i+1));
}
}
}