Skip to content

Practice Minimum Number of Operations to Make String Sorted

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#1830
LeetCode Problem

Minimum Number of Operations to Make String Sorted

Problem Statement

Repeat the following operation until s is sorted in nondecreasing order. Find the largest index i with 1 <= i < s.length and s[i] < s[i - 1]. Then choose the largest j from i onward such that every character from i through j is smaller than s[i - 1]. Swap s[i - 1] with s[j], and reverse the suffix beginning at i. Return the number of operations modulo 10^9 + 7.

Source: Minimum Number of Operations to Make String Sorted on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
s = "cba"
Output
5

# Example 2

Input
s = "aabaa"
Output
2

Constraints

1 <= s.length <= 3000
s​​​​​​ consists only of lowercase English letters.

Related LeetCode Problems

Practice more problems similar to Minimum Number of Operations to Make String Sorted.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.