Video summary
L13. Print all Permutations of a String/Array | Recursion | Approach - 2
Main summary
Key takeaways
Main ideas / lesson
- This video presents a recursion-based approach to generate all permutations of an array/string using in-place swapping (i.e., no extra map / no extra data structure for tracking used elements).
- The method works by fixing one position at a time (starting from a given
index) and recursively permuting the remaining suffix. - Backtracking is used: after exploring a choice via recursion, the algorithm undoes the swap to restore the array state.
Methodology (detailed step-by-step)
Core recursion strategy (swap-based)
Assume an array a of length n. Define a recursive function that takes:
index= the position currently being fixed- the array
a - an output container
answer(e.g., list/vector of permutations)
At recursion level where index is fixed:
- Loop variable
igoes fromindexton-1:- Swap
a[index]witha[i]- This choice means: “put the element currently at position
iinto the fixed positionindex.”
- This choice means: “put the element currently at position
- Recurse with
index + 1(fix the next position) - After recursion returns, swap back
a[index]anda[i]to undo the change (backtracking)
- Swap
Base case
- If
index == n(i.e., you have fixed all positions):- The current array
arepresents one complete permutation. - Add a copy of
atoanswer. - Return / stop recursion.
- The current array
Intuition behind swapping
- Each recursion level ensures that every number can occupy the current index by swapping it into place from the remaining range.
- By exploring all choices for each position and backtracking, the algorithm produces all possible permutations.
What permutations generation looks like (conceptual example)
- For a 3-element array
[1,2,3]:- Fix position 0 by swapping choices
(0 with 0),(0 with 1),(0 with 2) - For each such fixed prefix, fix position 1 similarly from the remaining elements
- For the last index, there’s only one remaining choice, and the recursion reaches the base case to record the permutation.
- Fix position 0 by swapping choices
The video walks through the recursion tree producing permutations like:
1 2 3,1 3 2,2 1 3,2 3 1,3 1 2,3 2 1
Complexity notes
-
Time complexity:
- Generates
n!permutations - For each permutation, copying/adding can cost O(n)
- Overall stated:
O(n! * n)
- Generates
-
Space complexity:
- Does not use a separate “used” map/array (unlike the prior approach mentioned).
- Extra space mainly from recursion depth: O(n)
- Output storage inherently requires O(n! * n) (permutation list size), but the recursion itself avoids extra auxiliary structures.
Implementation outline (Java / C++)
Java approach (high-level structure)
-
Use:
List<List<Integer>> answer- A recursive helper called initially with
index = 0
-
Base case:
- When
indexreaches the end, convert/store the currentnumsarrangement as aList<Integer>and add toanswer.
- When
-
Recursive step:
- For each
ifromindexton-1:- Swap
nums[index]andnums[i] - Recurse with
index + 1 - Swap back (backtracking)
- Swap
- For each
C++ approach (high-level structure)
- Return type:
vector<vector<int>> - Maintain:
vector<vector<int>> answer
-
Recursive helper:
- Parameters:
index,nums, andanswer
- Parameters:
-
Base case:
- If
index == n, push currentnumsintoanswer
- If
-
Recursive step:
- For
iin[index, n-1]:- swap
nums[index]andnums[i] - recurse on
index + 1 - swap back
- swap
- For
Speakers / sources featured
- No named speakers or external sources are mentioned in the provided subtitles (only an unnamed presenter/author narrating the explanation).
Rate this summary
Your feedback will help improve summaries.
Improve this summary
Reprocess with a stronger model when the summary feels incomplete or inaccurate.
Translate summary in another language
Ask questions to this video
Chat for follow-up questions, clarifications, and source-backed answers.