Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Set;
- import java.util.stream.Collectors;
- public class Test {
- public static Set<String> getPermutation(String str) {
- // create a set to avoid duplicate permutation
- Set<String> permutations = new HashSet<>();
- // check if string is null
- if (str == null) {
- return null;
- } else if (str.length() == 0) {
- // terminating condition for recursion
- permutations.add("");
- return permutations;
- }
- // get the first character
- char first = str.charAt(0);
- // get the remaining substring
- String sub = str.substring(1);
- // make recursive call to getPermutation()
- Set<String> words = getPermutation(sub);
- // access each element from words
- for (String strNew : words) {
- for (int i = 0;i<=strNew.length();i++){
- // insert the permutation to the set
- permutations.add(strNew.substring(0, i) + first + strNew.substring(i));
- }
- }
- return permutations;
- }
- public static void main(String[] args) {
- //findAllPossibleGreaterNumber();
- System.out.println(getPermutation("ABC"));
- }
- // private static void findAllPossibleGreaterNumber() {
- // int[] array = {17, 6, 9, 16, 5, 13, 8, 2, 1};
- // List<Integer> list= Arrays.stream(array).boxed().collect(Collectors.toList());
- // for (int i = 0; i <= array.length-1 ; i++) {
- // list.remove(Integer.valueOf(array[i]));
- // if(!list.isEmpty()) {
- // if (array[i] >= list.stream().max(Integer::compareTo).get())
- // System.out.println(array[i]);
- // }
- // }
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement