- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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[2] and s[4] to get s="ppppq", then reverse the substring from index 3. Now, s="pppqp".

In second operation, i=4, j=4. exchange s[3] and s[4] 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]

return a

Let us see the following implementation to get better understanding

def solve(s): d = [0]*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))

"ppqpp"

2

- Related Questions & Answers
- 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 find minimum operations to make the array increasing using Python
- Program to count minimum number of operations to flip columns to make target in Python
- Program to find minimum one bit operations to make integers zero in Python
- Program to count minimum number of operations required to make numbers non coprime in Python?
- Find minimum number of merge operations to make an array palindrome in C++
- 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 find minimum operations needed to make two arrays sum equal in Python
- Program to find minimum number of days to wait to make profit in python
- Program to find minimum number of days to make m bouquets using Python
- Program to find minimum number of monotonous string groups in Python

Advertisements