- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
Program to find minimum number of operations to make string sorted in Python
Suppose we have a string s. We have to perform the following operation on s until we get a sorted string −
Select largest index i such that 1 <= i < length of s and s[i] < s[i - 1].
Select largest index j such that i <= j < length of s and s[k] < s[i - 1] for all the possible values of k in the range [i, j] inclusive.
Exchange two characters at indices i - 1 and j.
Reverse the suffix from index i.
We have to find the number of operations required to make the string sorted. The answer may be very large so return result modulo 10^9 + 7.
So, if the input is like s = "ppqpp", then the output will be 2 because
In first operation, i=3, j=4. exchange s and s to get s="ppppq", then reverse the substring from index 3. Now, s="pppqp".
In second operation, i=4, j=4. exchange s and s to get s="ppppq", then reverse the substring from index 4, Now, s = "ppppq".
To solve this, we will follow these steps −
d := An array of size 26 and fill with 0
a := 0, t := 1
m := 10^9 + 7
n := ASCII of 'a'
for each index i and character c of s in reverse order, start index from 1, do
j := ASCII of c - n
d[j] := d[j] + 1
a :=(a + sum of all elements of d[from index 0 to j-1]) * quotient of t/d[j]) mod m
t := t * quotient of i/d[j]
Let us see the following implementation to get better understanding
def solve(s): d = *26 a = 0 t = 1 m = 10**9 + 7 n = ord('a') for i,c in enumerate(s[::-1],1): j = ord(c) - n d[j] += 1 a = (a+sum(d[:j])*t//d[j]) % m t = t*i//d[j] return a s = "ppqpp" print(solve(s))
- Related Articles
- Program to find minimum number of operations required to make one string substring of other in Python
- Program to find minimum number of operations required to make one number to another in Python
- Program to find minimum number of operations required to make lists strictly Increasing in python
- Program to find minimum operations to make array equal using Python
- Program to count minimum number of operations to flip columns to make target in Python
- Program to count minimum number of operations required to make numbers non coprime in Python?
- Program to find minimum one bit operations to make integers zero in Python
- Program to find minimum operations to make the array increasing using Python
- C++ program to find minimum how many operations needed to make number 0
- Program to find minimum operations needed to make two arrays sum equal in Python
- Program to find minimum deletions to make string balanced in Python
- Program to find equal sum arrays with minimum number of operations in Python
- Program to check minimum number of characters needed to make string palindrome in Python
- C++ program to count minimum number of operations needed to make number n to 1
- Program to count number of operations needed to make string as concatenation of same string twice in Python