- 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 lowercase string s with letters x and y. Now consider an operation in which we change a single x to y or vice versa. We have to find the minimum number of times we need to perform that operation to set all x's come before all y's.

So, if the input is like s = "yxyyyyxyxx", then the output will be 4.

To solve this, we will follow these steps −

y_left := 0

x_right := number of "x" in s, res := number of "x" in s

for each item in s, do

if item is same as "x", then

x_right := x_right − 1

otherwise,

y_left := y_left + 1

res := minimum of res and (y_left + x_right)

return res

Let us see the following implementation to get better understanding −

class Solution: def solve(self, s): y_left = 0 x_right = res = s.count("x") for item in s: if item == "x": x_right -= 1 else: y_left += 1 res = min(res, y_left + x_right) return res ob = Solution() s = "yxyyyyxyxx" print(ob.solve(s))

"yxyyyyxyxx"

4

- Related Questions & Answers
- Python program to remove each y occurrence before x in List
- Program to count number of minimum swaps required to make it palindrome in Python
- Program to count minimum number of operations required to make numbers non coprime in Python?
- Program to count number of swaps required to group all 1s together in Python
- Number of operations required to make all array elements Equal in Python
- Program to count number of operations required to all cells into same color in Python
- Program to count number of operations required to convert all values into same in Python?
- Python program to test if all y occur after x in List
- Program to find minimum number of operations required to make one number to another in Python
- Find number of pairs (x, y) in an array such that x^y > y^x in C++
- Count of pairs (x, y) in an array such that x < y in C++
- Program to find minimum number of operations required to make lists strictly Increasing in python
- Program to count number of swaps required to change one list to another in Python?
- Program to count number of switches that will be on after flipping by n persons in python
- Program to count number of 5-star reviews required to reach threshold percentage in Python

Advertisements