@@ -20,3 +20,74 @@ Input:
20201 . letters = abc
2121 - Output: abc acb bac bca cab cba
2222
23+
24+ ## Constraints
25+
26+ - All characters in word are unique.
27+ - 1 ≤ word.length ≤ 6
28+ - All characters in word are lowercase English letters.
29+
30+ ## Solution
31+
32+ Problems such as this one, where we need to find the combinations or permutations of a given string, are good examples
33+ to solve using the subsets pattern as it describes an efficient Depth-First Search (DFS) approach to handle all these
34+ problems.
35+
36+ Let’s discuss a few basics first. We know that n! is the number of permutations for a set of size n. Another obvious and
37+ important concept is that if we choose an element for the first position, then the total permutations of the remaining
38+ elements are (n−1)!.
39+
40+ For example, if we’re given the string “abcd” and we pick “a” as our first element, then for the remaining elements we
41+ have the following permutations:
42+
43+ ![ Solution Sample] ( ./images/solutions/generate_permutations_solution_sample.png )
44+
45+ Similarly, if we pick “b” as the first element, permute “acd”, and prepend each permutation with “b”, we can observe a
46+ pattern here as shown in the illustration above. That pattern tells us how to find all remaining permutations for each
47+ character in the given string.
48+
49+ We can do this recursively to find all permutations of substrings, such as “bcd”, “acd”, and so on. This implies that
50+ generating all possible permutations of the given string involves exploring different combinations of characters, which
51+ can be done efficiently using the subset technique. The key idea is to take one character of the given string at a time
52+ and find all the permutations that start with this chosen character. For this, imagine filling empty positions equal to
53+ the length of the string in the following manner: place the chosen character at the first position, then against this
54+ character, try all the remaining characters in the second position. Next, for each pair of characters in the first and
55+ second positions, try all the remaining characters in the third position. Keep doing this until we reach the last
56+ position to be filled. This process will allow us to systematically arrange each character in different positions and
57+ generate all possible permutations of the given string.
58+
59+ Here is a visual representation of all recursions for input string “bad”:
60+
61+ ![ Solution 1] ( ./images/solutions/generate_permutations_solution_1.png )
62+ ![ Solution 2] ( ./images/solutions/generate_permutations_solution_2.png )
63+ ![ Solution 3] ( ./images/solutions/generate_permutations_solution_3.png )
64+ ![ Solution 4] ( ./images/solutions/generate_permutations_solution_4.png )
65+ ![ Solution 5] ( ./images/solutions/generate_permutations_solution_5.png )
66+ ![ Solution 6] ( ./images/solutions/generate_permutations_solution_6.png )
67+ ![ Solution 7] ( ./images/solutions/generate_permutations_solution_7.png )
68+
69+ We create a recursive function to compute the permutations of the string that has been passed as input. The function
70+ behaves in the following way:
71+
72+ - We fix the first character of the input string and swap it with its immediate next character.
73+ - We swap the indexes and get a new permutation of the string, which is stored in the variable, swapped_str.
74+ - The recursive call for the function increments the index by adding 1 to the current_index variable to compute the next
75+ permutation.
76+ - All permutations of the string are stored in the result array
77+
78+ ### Time Complexity
79+
80+ Let’s anaylze the time complexity of the solution code above:
81+ - ` permute_word() ` : There are n! (factorial of n) permutations of a string of length n.
82+ - ` permute_string() ` : This is a recursive function that generates these permutations. For a string of length n, there
83+ are n recursive calls at the first level, ` n-1 ` calls for the second, and so on. This results in a total of
84+ ` n * (n-1) * (n-2) * ... * 1 = n! ` calls
85+ - ` swap_char() ` : The swap function has a time complexity of O(n) since it involves creating a list from the string and
86+ then joining it back into a string after the swap. This operation is done for each recursive call.
87+
88+ So the overall time complexity is O(n * n!)
89+
90+ ### Space Complexity
91+
92+ The space complexity of this solution is dependent on the depth of the recursive call stack. The maximum depth of
93+ recursion is n, so the space complexity is O(n).
0 commit comments