**Half Monotonous String - Google Top Interview Questions**

### Problem Statement :

You are given a lowercase alphabet string s of even length consisting of lowercase alphabet letters. Return the minimum number of characters that need to be updated such that one of the following three conditions is true for all 0 ≤ i < n/2 and n/2 ≤ j < n: s[i] > s[j] or s[i] < s[j] or s[i] == s[j] Constraints 0 ≤ n ≤ 100,000 where n is the length of s Example 1 Input s = "aa" Output 0 Explanation This string already meets the s[i] == s[j] condition Example 2 Input s = "aaabba" Output 1 Explanation If we change the last "a" to "b", then we can satisfy the s[i] < s[j] condition. Example 3 Input s = "bccbba" Output 1 Explanation If we change the first "b" to "c", then we can satisfy the s[i] > s[j] condition.

### Solution :

` ````
Solution in C++ :
int solve(string s) {
int len = s.length(), maxCnt = 0;
vector<int> cnt(26, 0);
for (int i = 0; i < len; i++) {
cnt[s[i] - 'a']++;
maxCnt = max(maxCnt, cnt[s[i] - 'a']);
}
int res = len - maxCnt;
for (char ch = 'b'; ch < 'z'; ch++) {
int cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0;
for (int i = 0; i < len / 2; i++) {
if (s[i] >= ch) {
cnt1++;
} else if (s[i] <= ch - 1) {
cnt2++;
}
}
for (int i = len / 2; i < len; i++) {
if (s[i] >= ch) {
cnt3++;
}
if (s[i] <= ch - 1) {
cnt4++;
}
}
res = min({res, cnt1 + cnt4, cnt2 + cnt3});
}
return res;
}
```

` ````
Solution in Java :
import java.util.*;
class Solution {
public int solve(String s) {
int N = s.length();
int[] A = new int[26];
int[] B = new int[26];
for (int i = 0; i < N; i++) {
int c = s.charAt(i) - 'a';
if (i < N / 2)
A[c] += 1;
else
B[c] += 1;
}
int ans = Integer.MAX_VALUE;
// fix all characters in A to be greater than i and all the characters in B to be less than
// or equal to i.
for (int i = 0; i < 26; i++) {
int fix = 0;
for (int j = 0; j <= i; j++) fix += A[j];
for (int j = i + 1; j < 26; j++) fix += B[j];
ans = Math.min(ans, fix);
}
// fix all characters in A to be less than i and all the characters in B to be greater than
// or equal to i.
for (int i = 0; i < 26; i++) {
int fix = 0;
for (int j = i; j < 26; j++) fix += A[j];
for (int j = 0; j < i; j++) fix += B[j];
ans = Math.min(ans, fix);
}
// fix all characters in A and B to equal i.
for (int i = 0; i < 26; i++) {
int fix = N - (A[i] + B[i]);
ans = Math.min(ans, fix);
}
return ans;
}
}
```

` ````
Solution in Python :
class Solution:
def solve(self, s):
n = len(s)
left = Counter(s[: n >> 1])
right = Counter(s[n >> 1 :])
ans = n
for pivot in ascii_lowercase:
# All same
ans = min(ans, n - left[pivot] - right[pivot])
# left <= pivot < right
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)
# right <= pivot < left
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
```

## View More Similar Problems

## Queue using Two Stacks

A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed. A basic que

View Solution →## Castle on the Grid

You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal. Function Description Complete the minimumMoves function in the editor. minimumMoves has the following parameter(s):

View Solution →## Down to Zero II

You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations N on in each move: 1: If we take 2 integers a and b where , N = a * b , then we can change N = max( a, b ) 2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0. Input Format The first line contains the integer Q.

View Solution →## Truck Tour

Suppose there is a circle. There are N petrol pumps on that circle. Petrol pumps are numbered 0 to (N-1) (both inclusive). You have two pieces of information corresponding to each of the petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance from that petrol pump to the next petrol pump. Initially, you have a tank of infinite capacity carrying no petr

View Solution →## Queries with Fixed Length

Consider an -integer sequence, . We perform a query on by using an integer, , to calculate the result of the following expression: In other words, if we let , then you need to calculate . Given and queries, return a list of answers to each query. Example The first query uses all of the subarrays of length : . The maxima of the subarrays are . The minimum of these is . The secon

View Solution →## QHEAP1

This question is designed to help you get a better understanding of basic heap operations. You will be given queries of types: " 1 v " - Add an element to the heap. " 2 v " - Delete the element from the heap. "3" - Print the minimum of all the elements in the heap. NOTE: It is guaranteed that the element to be deleted will be there in the heap. Also, at any instant, only distinct element

View Solution →