-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPermutationsOfAGivenString.java
More file actions
55 lines (41 loc) · 1.09 KB
/
PermutationsOfAGivenString.java
File metadata and controls
55 lines (41 loc) · 1.09 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
// http://practice.geeksforgeeks.org/problems/permutations-of-a-given-string/0
import java.util.*;
class PermutationsOfAGivenString {
public static Vector<String> vec = new Vector<String>();
public static void print(char str[],int i,int j)
{
if(i==j) {
String temp=new String(str);
vec.add(temp);
} else {
char a=str[i];
for(int k=i;k<=j;k++) {
str[i]=str[k];
str[k]=a;
print(str,i+1,j);
str[k]=str[i];
str[i]=a;
}
}
}
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-- > 0) {
String str=sc.next();
char arr[]=new char[str.length()];
arr = str.toCharArray();
print(arr,0,str.length()-1);
for(int i=0;i<vec.size();i++) {
System.out.print(vec.get(i)+" ");
}
System.out.println();
Collections.sort(vec);
for(int i=0;i<vec.size();i++) {
System.out.print(vec.get(i)+" ");
}
System.out.println("");
vec.clear();
}
}
}