3863. Minimum Operations to Sort a String
Description
You are given a string s consisting of lowercase English letters.
In one operation, you can select any substring of s that is not the entire string and sort it in non-descending alphabetical order.
Return the minimum number of operations required to make s sorted in non-descending order. If it is not possible, return -1.
Example 1:
Input: s = "dog"
Output: 1
Explanation:
- Sort substring
"og"to"go". - Now,
s = "dgo", which is sorted in ascending order. Thus, the answer is 1.
Example 2:
Input: s = "card"
Output: 2
Explanation:
- Sort substring
"car"to"acr", sos = "acrd". - Sort substring
"rd"to"dr", makings = "acdr", which is sorted in ascending order. Thus, the answer is 2.
Example 3:
Input: s = "gf"
Output: -1
Explanation:
- It is impossible to sort
sunder the given constraints. Thus, the answer is -1.
Constraints:
1 <= s.length <= 105sconsists of only lowercase English letters.
Solutions
Solution 1: Case Analysis
Thinking
Each operation sorts a proper substring. We want the fewest operations to make \(s\) nondecreasing. \(|s| \le 10^5\) forbids simulating sorts.
One operation can sort any proper substring, so the answer depends only on whether the min and max letters can be moved to the ends in one or two steps.
Already sorted yields \(0\). A disordered length-\(2\) string has no proper substring covering both letters, hence \(-1\). Min already first or max already last needs one operation; one of them in the interior needs two; otherwise three.
The case split is exhaustive and checked in linear time.
We first check whether the string is already sorted in ascending order; if so, return 0.
Otherwise, if the string has length 2, since we cannot choose the entire string to sort, it is impossible to sort the string, so we return -1.
Next, we find the minimum character \(mn\) and the maximum character \(mx\) in the string. If the first character of the string equals \(mn\), or the last character equals \(mx\), then one operation on the remaining substring is sufficient to sort the entire string, so we return 1.
Otherwise, if some character in the middle of the string equals \(mn\) or \(mx\), we need one operation to move that character to the beginning or the end of the string, and then one more operation to sort the rest, so we return 2.
Finally, if none of the above cases apply, we need one operation on the substring containing both \(mn\) and \(mx\), followed by one more operation on the remaining substring, so we return 3.
The time complexity is \(O(n)\), where \(n\) is the length of the string \(s\). The space complexity is \(O(1)\).
1 2 3 4 5 6 7 8 9 10 11 12 | |
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 | |
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 | |
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 | |
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 | |