Handle Score
@Ibrahim Nash 6420
@blackshadows 6380
@mb1973 5704
@Quandray 5245
@akhayrutdinov 5111
@saiujwal13083 5046
@sanjay05 3762
@kirtidee18 3673
@mantu_singh 3530
@marius_valentin_dragoi 3523
@sushant_a 3459
Complete Leaderboard
Lexicographically Previous Permutation
Easy Accuracy: 55.89% Submissions: 161 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)

1 ≤ |s| ≤ 103

to report an issue on this page.


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


All Submissions

My Submissions:

Login to access your submissions.

Lexicographically Previous Permutation

Output Window