Given a string, list all possible combinations and permutations of its characters.
getCombPerms("a") ==> {"a"}
getCombPerms("ab") ==> {"a","ab","ba","b"}
Fix each letter in the string as the first letter and find all the permutations of the remaining letters using recursive calls.
The base case for recursion is : when the input is an empty String
, null
or a single character, the only permutation is the input itself.
1. Initialize an ArrayList
to store all the permutations.
2. Check for the base conditions i.e. if the input is null
, return the input itself. If the input is a single Character
or an empty String
, add it to the list.
3. Separate out the first character and recursively find the permutation of the remainder String
.
4. Iterate and insert the separated character at every position in all the words obtained from the recursive call.
5. Return the list of all the words.
public static ArrayList<String> getCombPerms(String s) { }
C
Java
Python