Advertisement

Minimum Number of Operations to Make String Sorted - LeetCode 1830 Solution

Minimum Number of Operations to Make String Sorted - Complete Solution Guide

Minimum Number of Operations to Make String Sorted is LeetCode problem 1830, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 that 1 <= i < s.length and s[i] < s[i - 1] . Find the largest index j such that i <= j < s.length and s[k] < s[i - 1] for all the possible values of k in the range [i, j] inclusive. Swap the two characters at indices i - 1 ​​​​ and j ​​​​​. Reverse the suffix starting at index i ​​​​​​. Return the number of operations needed to ma

Detailed Explanation

The problem asks us to find the number of operations required to sort a given string 's' using a specific set of operations. The operations involve finding the largest index 'i' where s[i] < s[i-1], finding the largest index 'j' >= 'i' where s[j] < s[i-1], swapping s[i-1] and s[j], and then reversing the suffix of the string starting at index 'i'. We need to repeat these operations until the string is sorted and return the number of operations modulo 10^9 + 7.

Solution Approach

The solution computes the number of permutations smaller than the given string by iterating through the string from left to right. For each character, it determines the number of characters to its right that are smaller than it. Then, it calculates the number of permutations formed by those smaller characters, taking into account the repetitions of characters. A binary indexed tree (BIT) is used to efficiently count the number of smaller characters and update their frequencies. Factorials and their modular inverses are precomputed to speed up the permutation calculation.

Step-by-Step Algorithm

  1. Step 1: Precompute factorials and inverse factorials modulo 10^9 + 7 up to the length of the string.
  2. Step 2: Initialize a frequency array (counts) to store the count of each character in the string.
  3. Step 3: Initialize a Binary Indexed Tree (BIT) with size 26 to store cumulative frequency of characters.
  4. Step 4: Iterate through the string from left to right.
  5. Step 5: For each character at index i, query the BIT to find the number of characters smaller than s[i] that appear later in the string.
  6. Step 6: Calculate the number of permutations that can be formed using the characters to the right of s[i]. This involves multiplying the factorial of the length of the remaining string by the modular inverse of the product of the factorials of the character counts.
  7. Step 7: Add the contribution of the current character to the total operations.
  8. Step 8: Update the BIT and counts array to reflect the removal of the current character.
  9. Step 9: Return the total operations modulo 10^9 + 7.

Key Insights

  • Insight 1: The given operations simulate finding the lexicographically next permutation of the string. So, we need to find how many permutations are there before the sorted permutation.
  • Insight 2: The problem can be solved by calculating the number of permutations smaller than the given string 's'. This requires calculating factorials, inverse factorials, and using a binary indexed tree (BIT) or frequency array to count the number of characters smaller than the current character.
  • Insight 3: Efficiently calculating factorials and their inverses modulo a large prime is crucial to avoid integer overflow and get the correct result.

Complexity Analysis

Time Complexity: O(n log n)

Space Complexity: O(n)

Topics

This problem involves: Math, String, Combinatorics.

Companies

Asked at: Samsung.