Given a string with no duplicate characters, return a list with all permutations of the string and all its subsets.
Examples
Set = “abc”, all permutations are [“”, “a”, “ab”, “abc”, “ac”, “acb”, “b”, “ba”, “bac”, “bc”, “bca”, “c”, “cb”, “cba”, “ca”, “cab”].
Set = “”, all permutations are [“”].
Set = null, all permutations are [].
public class Solution { public List<String> allPermutationsOfSubsets(String set) { // Write your solution here List<String> list = new ArrayList<>(); Set<Character> charSet = new HashSet<>(); helper(set, charSet, 0, list, new StringBuilder()); return list; } private void helper(String str, Set<Character> charSet, int num, List<String> result, StringBuilder sb) { result.add(sb.toString()); for (int i = 0; i < str.length(); i++) { if (charSet.contains(str.charAt(i))) { continue; } sb.append(str.charAt(i)); charSet.add(str.charAt(i)); helper(str, charSet, num + 1, result, sb); sb.deleteCharAt(sb.length() - 1); charSet.remove(str.charAt(i)); } } }