Leaderboard
Showing:
Handle Score
@Ibrahim Nash 6420
@blackshadows 6380
@mb1973 5642
@Quandray 5233
@akhayrutdinov 5111
@saiujwal13083 4610
@sanjay05 3762
@kirtidee18 3673
@marius_valentin_dragoi 3522
@sushant_a 3459
@verma_ji 3413
Complete Leaderboard
Lexicographically Previous Permutation
Easy Accuracy: 54.44% Submissions: 101 Points: 2

Given a string s, find lexicographically previous permutation of it.
For example: Lexicographically previous permutation of “4321” is “4312". If the string is sorted in ascending order, then the lexicographically previous permutation doesn’t exist.

Example 1:

Input: s = "4321"
Output: "4312" 
Explanation: lexicographically previous
permutation of “4321” is “4312”

Example 2:

Input: s = "1234"
Output: "None"
Explanation: "1234" is sorted in
ascending order, hence "None". 

Your Task:  
You dont need to read input or print anything. Complete the function prevPermutation() which takes string s as input parameter and returns lexicographically previous permutation of s and if there is no lexicographically previous permutation of s, it returns "None".

Expected Time Complexity: O(|s|)
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ |s| ≤ 103

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Lexicographically Previous Permutation

Output Window