Program to find how many updates required to make string half monotonous in Python

Suppose we have a lowercase string s whose length is even. We have to find the minimum number of characters that need to be updated such that one of the following three conditions is satisfied for all i, where 0 ? i < n/2 and j, n/2 ? j < n −

  • s[i] > s[j] (left half characters greater than right half)
  • s[i] < s[j] (left half characters less than right half)
  • s[i] == s[j] (left half characters equal to right half)

So, if the input is like s = "pppxxp", then the output will be 1 because if we change the last "p" to "x", then this can satisfy the condition s[i] < s[j].

Algorithm

To solve this, we will follow these steps −

  • n := size of s
  • left := a dictionary containing frequencies of each character from the left half of s
  • right := a dictionary containing frequencies of each character from the right half of s
  • ans := n
  • for each character pivot in lowercase English letters, do
    • ans := minimum of ans and (n - left[pivot] - right[pivot])
    • good := sum of all elements present in (left[c] for each c in left if c ? pivot )
    • good := good + sum of all elements present in right[c] for each c in right if c > pivot
    • ans := minimum of ans and (n - good)
    • good := sum of all elements present in left[c] for each c in left if c > pivot
    • good := good + sum of all elements present in right[c] for each c in right if c ? pivot
    • ans := minimum of ans and (n - good)
  • return ans

Example

Let us see the following implementation to get better understanding −

from collections import Counter
from string import ascii_lowercase

def solve(s):
    n = len(s)
    left = Counter(s[: n >> 1])
    right = Counter(s[n >> 1 :])

    ans = n
    for pivot in ascii_lowercase:
        # Case 1: All characters are equal to pivot
        ans = min(ans, n - left[pivot] - right[pivot])

        # Case 2: left[i] <= pivot and right[j] > pivot
        good = sum(left[c] for c in left if c <= pivot)
        good += sum(right[c] for c in right if c > pivot)
        ans = min(ans, n - good)

        # Case 3: left[i] > pivot and right[j] <= pivot
        good = sum(left[c] for c in left if c > pivot)
        good += sum(right[c] for c in right if c <= pivot)
        ans = min(ans, n - good)

    return ans

s = "pppxxp"
print(solve(s))

The output of the above code is −

1

How It Works

The algorithm considers all possible pivot characters and calculates three scenarios:

  • Equal condition: Make all characters in both halves equal to the pivot character
  • Less-than condition: Make left half ? pivot and right half > pivot
  • Greater-than condition: Make left half > pivot and right half ? pivot

For the input "pppxxp", the left half is "ppp" and right half is "xxp". The algorithm finds that changing one character (the last 'p' to 'x') satisfies the less-than condition.

Conclusion

This algorithm efficiently finds the minimum updates needed to make a string half-monotonous by trying all possible pivot characters and calculating the cost for each of the three valid conditions. The time complexity is O(26n) which simplifies to O(n).

Updated on: 2026-03-26T17:17:52+05:30

219 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements