-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericRecursionEngine.java
More file actions
109 lines (99 loc) · 3.86 KB
/
GenericRecursionEngine.java
File metadata and controls
109 lines (99 loc) · 3.86 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package com.example.recursion;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import com.google.common.collect.HashMultiset;
public class GenericRecursionEngine<T> {
/**
* Return all subsets of a given set of elements of type T.
*/
public HashSet<HashSet<T>> getSubsets(final HashSet<T> rootSet) {
final HashSet<HashSet<T>> setOfSubsets = new HashSet<HashSet<T>>();
getSubsets(rootSet, setOfSubsets);
return setOfSubsets;
}
/**
* Wrapper for getSubsets(HashSet<T> rootSet) public method.
*/
private void getSubsets(final HashSet<T> rootSet, final Set<HashSet<T>> setOfSubsets) {
setOfSubsets.add(rootSet);
HashSet<T> tempSet = null;
for (T element : rootSet) {
tempSet = new HashSet<T>(rootSet);
tempSet.remove(element);
if (!tempSet.isEmpty()) getSubsets(tempSet, setOfSubsets);
}
}
/**
* Return all permutations of a given string.
*/
public HashSet<StringBuilder> getPermutations(StringBuilder string) {
HashSet<StringBuilder> setOfPermutations = new HashSet<StringBuilder>();
HashSet<HashMap<Character, Integer>> rootSet = new HashSet<HashMap<Character, Integer>>();
HashMap<Character, Integer> hash = null;
for (int i= 0; i < string.length(); i++) {
hash = new HashMap<Character, Integer>();
hash.put(string.charAt(i), i);
rootSet.add(hash);
}
getPermutations(null, rootSet, setOfPermutations);
return setOfPermutations;
}
private void getPermutations(final StringBuilder rootString,
final HashSet<HashMap<Character, Integer>> rootSet,
final HashSet<StringBuilder> setOfPermutations) {
StringBuilder tempRootString = null;
for (HashMap<Character, Integer> hash : rootSet) {
HashSet<HashMap<Character, Integer>> tempSet =
new HashSet<HashMap<Character, Integer>>(rootSet);
tempSet.remove(hash);
for (Character ch : hash.keySet()) {
if (rootString == null) tempRootString = new StringBuilder();
else tempRootString = new StringBuilder(rootString);
tempRootString.append(ch);
if (tempSet.isEmpty()) {
setOfPermutations.add(tempRootString);
} else getPermutations(tempRootString, tempSet, setOfPermutations);
}
}
}
/**
* Return all permutations of a given
* string using Google common package.
*/
public HashSet<StringBuilder> getPermutationsWithBag(StringBuilder string) {
HashSet<StringBuilder> setOfPermutations = new HashSet<StringBuilder>();
HashMultiset<Character> multiset = HashMultiset.create();
for (int i= 0; i < string.length(); i++)
multiset.add(string.charAt(i));
getPermutationsWithBag(null, multiset, setOfPermutations);
return setOfPermutations;
}
private void getPermutationsWithBag(final StringBuilder rootString,
final HashMultiset<Character> rootMultiset,
final HashSet<StringBuilder> setOfPermutations) {
StringBuilder tempRootString = null;
HashMultiset<Character> tempMultiset = null;
for (Character ch : rootMultiset) {
tempMultiset = HashMultiset.create(rootMultiset);
tempMultiset.remove(ch);
if (rootString == null) tempRootString = new StringBuilder();
else tempRootString = new StringBuilder(rootString);
tempRootString.append(ch);
if (tempMultiset.isEmpty()) {
setOfPermutations.add(tempRootString);
} else getPermutationsWithBag(tempRootString, tempMultiset, setOfPermutations);
}
}
public long swapInPlace(long value) {
int numberOfDigits = 0;
while ((int)(value / Math.pow(10, numberOfDigits)) != 0) numberOfDigits++;
int k = 0;
long result = value;
while (k < numberOfDigits) {
result += (value % Math.pow(10, k + 1) - value % Math.pow(10, k)) * Math.pow(10, 2 * (numberOfDigits - k) - 1);
k++;
}
return (long) (result / Math.pow(10, numberOfDigits));
}
}