Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [[1,2]] == [[2,1]]
- [[1],[2]] == [[2],[1]]
- [[1,2],[3,4]] == [[2,1],[4,3]] == [[4,3],[2,1]]...
- [[1,2]]
- [[1],[2]]
- [[1,2,3]]
- [[1,2],[3]]
- [[1,3],[2]]
- [[2,3],[1]]
- [[1],[2],[3]]
- [[1,2,3,4]]
- [[1,2,3],[4]]
- [[1,2,4],[3]]
- [[1,3,4],[2]]
- [[2,3,4],[1]]
- [[1,2],[3,4]] <= Equivalent to [[3,4],[1,2]]
- [[1,3],[2,4]] <= Equivalent to [[2,4],[1,3]]
- [[1,4],[2,3]] <= Equivalent to [[2,3],[1,4]]
- [[1,2],[3],[4]]
- [[1,3],[2],[4]]
- [[1,4],[2],[3]]
- [[2,3],[1],[4]]
- [[2,4],[1],[3]]
- [[3,4],[1],[2]]
- [[1],[2],[3],[4]]
- public Set<Set<Set<Integer>>> recurseGroups(List<Integer> initialArray, List<Set<Integer>> currentGroups, int i) {
- if (i == initialArray.size()) {
- // Deep copy current group to save its content
- Set<Set<Integer>> copy = currentGroups.stream()
- .map(HashSet::new)
- .collect(Collectors.toSet());
- return Collections.singleton(copy);
- }
- Set<Set<Set<Integer>>> result = new HashSet<>();
- for (int j = i; j < initialArray.size(); ++j) {
- // Add a singleton group with the i-th integer
- // And generate groups with the remaining elements
- Set<Integer> newGroup = new HashSet<>(Collections.singletonList(initialArray.get(i)));
- currentGroups.add(newGroup);
- result.addAll(recurseGroups(initialArray, currentGroups, i + 1));
- currentGroups.remove(newGroup);
- // Add the i-th integer to each previous existing groups
- // And generate groups with the remaining elements
- for (int k = 0; k < currentGroups.size(); ++k) {
- currentGroups.get(k).add(initialArray.get(i));
- result.addAll(recurseGroups(initialArray, currentGroups, i + 1));
- currentGroups.get(k).remove(initialArray.get(i));
- }
- }
- return result;
- }
- // Calling line
- Set<Set<Set<Integer>>> groups = recurseGroups(Arrays.asList(1,2,3), new ArrayList<>(), 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement