Recursively print all permutations of a string Javascript
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Understanding String Permutations in JavaScript
Permutations are a fundamental concept in combinatorial mathematics, representing all possible arrangements of a set of elements. When working with strings in JavaScript, generating permutations involves systematically arranging all the characters of the string in every possible order. This can be useful in numerous applications, such as solving puzzles, generating test cases, or even cryptography.
Technical Explanation
To recursively find all permutations of a string in JavaScript, you must consider each character's position and recursively arrange the remaining characters. The key idea here is to fix one character and permute the rest of the characters. By recursively applying this logic, you can generate all possible permutations.
Here's the approach broken down:
- Base Case: If the string's length is 1, return an array containing the string itself, as there is only one permutation.
- Recursive Case:
- Iterate through the string, fixing each character and permuting the remaining substring.
- Use recursive calls to generate permutations of the remaining substring.
- Concatenate the fixed character with each permutation of the remaining substring.
JavaScript Implementation
Below is the JavaScript function implementing recursive permutations.
- String Slicing: Each character acts as a potential initial character for the permutations. The remaining characters (`remainingChars`) are generated using the `slice` method, which creates a substring excluding the fixed character.
- Recursive Permutation Generation: For each fixed character, the function recursively solves the subproblem of generating all permutations for the `remainingChars`.
- Concatenation: Each permutation returned by the recursive call is prefixed with the current fixed character to create the complete permutation of the original string.
- Handling Large Inputs: The permutation generation function is infeasible for large strings due to factorial growth in the number of permutations. Be cautious of performance issues and opt for alternative methods if feasible.
- Applications: String permutations are applicable in solving problems like the traveling salesperson problem, finding anagrams, cryptographic algorithms, and providing comprehensive test coverage in software testing.
Related reading

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.