mirror of
https://github.com/kamranahmedse/developer-roadmap.git
synced 2025-08-17 22:51:16 +02:00
Merge pull request #5722 from zongsforce/patch-2
modify the sample code
This commit is contained in:
@@ -3,9 +3,24 @@
|
|||||||
Factorial complexity algorithms have a runtime of `O(n!)`. This is the worst case scenario for an algorithm. Factorial complexity algorithms are very inefficient and should be avoided.
|
Factorial complexity algorithms have a runtime of `O(n!)`. This is the worst case scenario for an algorithm. Factorial complexity algorithms are very inefficient and should be avoided.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def factorial(n):
|
def generate_permutations(s):
|
||||||
if n == 0:
|
# Base case: If the string length is 1, return a list containing the string
|
||||||
return 1
|
if len(s) == 1:
|
||||||
else:
|
return [s]
|
||||||
return n * factorial(n-1)
|
|
||||||
|
# Initialize the result list
|
||||||
|
permutations = []
|
||||||
|
|
||||||
|
# Recursively generate all permutations
|
||||||
|
for i in range(len(s)):
|
||||||
|
# Current character
|
||||||
|
current_char = s[i]
|
||||||
|
# Remaining characters
|
||||||
|
remaining_chars = s[:i] + s[i+1:]
|
||||||
|
# Generate all permutations of the remaining characters
|
||||||
|
for perm in generate_permutations(remaining_chars):
|
||||||
|
# Add the current character to the front of each generated permutation
|
||||||
|
permutations.append(current_char + perm)
|
||||||
|
|
||||||
|
return permutations
|
||||||
```
|
```
|
||||||
|
Reference in New Issue
Block a user