-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringPermutations.java
More file actions
77 lines (64 loc) · 1.7 KB
/
Copy pathStringPermutations.java
File metadata and controls
77 lines (64 loc) · 1.7 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class StringPermutations {
static void permute(char a[] , int n)
{
TreeMap<Character , Integer> hm = new TreeMap();
for(char c : a)
{
if(hm.containsKey(c))
{
int tp = hm.get(c);
hm.remove(c);
hm.put(c,tp+1);
}
else
hm.put(c , 1);
}
char str[] = new char[hm.size()];
int count[] = new int[hm.size()];
int index = 0;
for(Map.Entry<Character,Integer> set : hm.entrySet())
{
str[index] = set.getKey();
count[index] = set.getValue();
index++;
}
char result[] = new char[a.length];
permuteUtil(str,count,result,0);
}
static void permuteUtil(char str[] , int count[], char result[], int level)
{
if(level == result.length)
{
printArray(result);
return;
}
for(int i = 0; i < str.length; i++)
{
if(count[i] == 0)
continue;
count[i]--;
result[level] = str[i];
permuteUtil(str,count,result,level+1);
count[i]++;
}
}
static void printArray(char result[])
{
for(char c : result)
System.out.print(c);
System.out.println();
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter String");
String s = in.next();
char a[] = s.toCharArray();
permute(a , a.length);
}
}