-
Notifications
You must be signed in to change notification settings - Fork 22
/
AllAnagrams.java
52 lines (41 loc) · 1.72 KB
/
AllAnagrams.java
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
package com.company.amazon;
import java.util.Map;
import java.util.stream.Collectors;
import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.groupingBy;
public class AllAnagrams {
public static void main(String[] args) {
String input = "CAT";
printAllAnagrams(input.toCharArray(), getFrequencyOfCharactersInInput(input), 0, new char[3]);
}
public static Map<Character, Long> getFrequencyOfCharactersInInput(String input) {
return input.chars() // Int Stream
.mapToObj(c -> (char) c)// Convert IntStream to Stream<Characters>
.collect(groupingBy(c -> c, counting()));
}
public static void printResult(char[] result) {
for (int i = 0; i < result.length; i++) {
System.out.print(result[i] + " ");
}
System.out.println();
}
public static void printAllAnagrams(char[] input, Map<Character, Long> frequency, int level, char[] result) {
if (input.length == level) {
printResult(result);
return;
}
// Start from Left To Right
for (int i = 0; i < input.length; i++) {
// No words left to be added in the result set
if (frequency.get(input[i]) == 0) {
continue;
}
result[level] = input[i];
// Since this character is used in this level so at next level it's frequency reduced by 1
frequency.put(input[i], frequency.get(input[i]) - 1);
printAllAnagrams(input, frequency, level + 1, result);
// We have traversed the level so restore the frequency
frequency.put(input[i], frequency.get(input[i]) + 1);
}
}
}