-
-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathNext_Permutation_of_an_array.py
47 lines (34 loc) · 1.13 KB
/
Next_Permutation_of_an_array.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
def next_permutation(L):
'''
Permute the list L in-place to generate the next lexicographic permutation.
Return True if such a permutation exists, else return False.
'''
n = len(L)
#------------------------------------------------------------
# Step 1: find rightmost position i such that L[i] < L[i+1]
i = n - 2
while i >= 0 and L[i] >= L[i+1]:
i -= 1
if i == -1:
return False
#------------------------------------------------------------
# Step 2: find rightmost position j to the right of i such that L[j] > L[i]
j = i + 1
while j < n and L[j] > L[i]:
j += 1
j -= 1
#------------------------------------------------------------
# Step 3: swap L[i] and L[j]
L[i], L[j] = L[j], L[i]
#------------------------------------------------------------
# Step 4: reverse everything to the right of i
left = i + 1
right = n - 1
while left < right:
L[left], L[right] = L[right], L[left]
left += 1
right -= 1
return True
L=list(input())
next_permutation(L)
print(*L)