Skip to content

Commit c57efdb

Browse files
authored
Create Permutations.py
1 parent 28b2a11 commit c57efdb

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

Linked_Lists/Permutations.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def permute(self, arr: List[int]) -> List[List[int]]:
3+
if len(arr) == 0:
4+
return []
5+
elif len(arr) == 1:
6+
return [arr]
7+
else:
8+
ans = []
9+
for i in range(len(arr)):
10+
curr_elem = arr[i]
11+
# Remaining elements before and after curr_elem as list
12+
rem = arr[:i] + arr[i+1:]
13+
# Generate permutations of the remaining
14+
for el in self.permute(rem):
15+
ans.append([curr_elem] + el)
16+
17+
return ans

0 commit comments

Comments
 (0)