mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2025-12-19 10:07:46 -05:00
improve permutation generator description (#64723)
Co-authored-by: Dario <105294544+Dario-DC@users.noreply.github.com>
This commit is contained in:
@@ -9,12 +9,20 @@ dashedName: build-a-permutation-generator
|
||||
|
||||
In this lab, you will build a permutation generator that will take a string and return all possible permutations of the characters in the string. For example, the possible permutations of the string `cat` are `cat`, `cta`, `act`, `atc`, `tac`, and `tca`.
|
||||
|
||||
The recursive way of creating permutations of a string works by storing the fixed starting part of the string (prefix), and creating permutations of the rest.
|
||||
|
||||
For example, let's consider the word `machine`. The first round of creating permutations would be made fixing the `m` as the prefix of the string, and then creating permutations of the rest of the string, `achine`.
|
||||
|
||||
For the rest of the string, permutations continue in the same way. One letter is added to the prefix, maybe the `c`, so the prefix becomes `mc`. Then, each of the permutations of `ahine` is concatenated to the prefix.
|
||||
|
||||
This continues until the prefix has all the letters, and the rest of the string is empty, that means one permutation has been created.
|
||||
|
||||
**Objective:** Fulfill the user stories below and get all the tests to pass to complete the lab.
|
||||
|
||||
**User Stories:**
|
||||
|
||||
1. You should create a function named `permuteString`.
|
||||
2. The `permuteString` function should take three parameters- a string, a prefix value and an empty array for storing and returning the results. The prefix value would accumulate characters to form a permutation.
|
||||
2. The `permuteString` function should take three parameters: a string, a prefix value and an empty array for storing and returning the results. The prefix value should be a string used to accumulate characters to form a permutation.
|
||||
3. Inside the function, you should check if the length of the passed string is `0`. If it is, push the current prefix to the results and return the results.
|
||||
4. Iterate over each character in the input string and for each iteration, remove the current character from the string and call the `permuteString` function recursively with updated arguments to build the remaining permutations.
|
||||
5. You should return the final results array.
|
||||
|
||||
Reference in New Issue
Block a user