1830. Minimum Number of Operations to Make String Sorted
https://leetcode.com/problems/minimum-number-of-operations-to-make-string-sorted
Description
You are given a string s
(0-indexed). You are asked to perform the following operation on s
until you get a sorted string:
Find the largest index
i
such that1 <= i < s.length
ands[i] < s[i - 1]
.Find the largest index
j
such thati <= j < s.length
ands[k] < s[i - 1]
for all the possible values ofk
in the range[i, j]
inclusive.Swap the two characters at indices
i - 1
andj
.Reverse the suffix starting at index
i
.
Return the number of operations needed to make the string sorted. Since the answer can be too large, return it modulo 109 + 7
.
Example 1:
Example 2:
Example 3:
Example 4:
Constraints:
1 <= s.length <= 3000
s
consists only of lowercase English letters.
ac
Last updated