Count Fair pairs in a String with Uppercase-Lowercase operations
Last Updated :
18 Apr, 2023
Given a string S of length N that contains only uppercase and lowercase letters, the task is to find the maximum number of pairs that consist of an uppercase and a lowercase version of the same letter, and each character can only belong to one pair. You are allowed to perform K operations, wherein each operation, you can either change an uppercase letter to a lowercase or vice-versa.
Examples:
Input: S = “xyyxzzYYZ”, K = 1
Output: 3
Explanation: One possible operation is to change the “x” to “X”, resulting in the pair “xX”. The maximum number of pairs that can be formed after this operation is 3 (xX, yY, Zz).
Input: S = “xyyXzzYyxxZ”, K = 2
Output: 5
Approach: This can be solved with the following idea:
The approach is to first create a map to store the frequency of each character in the given string. Then, we iterate through each letter of the alphabet (a to z). For each letter, we count the number of pairs of the same letter by subtracting the frequency of lowercase and uppercase letters. If there are still uppercase letters remaining, we count pairs of different letters with an uppercase letter. Similarly, if there are still lowercase letters remaining, we count pairs of different letters with a lowercase letter. We repeat these steps for each letter of the alphabet. Finally, the total number of pairs is the sum of pairs counted in steps 3-5 for each letter of the alphabet.
Steps to implement the approach:
- Initialize a map mp to store the frequency of each character in the input string.
- Initialize xxx and yyy to the characters ‘a’ and ‘A’, respectively, and output to zero.
- For each letter xxx in the range ‘a’ to ‘z’:
- Count the frequency of lowercase and uppercase versions of the letter.
- Count the number of pairs of the same letter (e.g. “Aa” or “Bb”) and subtract this count from the frequency of lowercase and uppercase letters.
- If there are still uppercase letters remaining, count pairs of different letters with an uppercase letter.
- If there are still lowercase letters remaining, count pairs of different letters with a lowercase letter.
- Add the total count of pairs for this letter to output.
- Increment xxx and yyy to move to the next letter in the alphabet.
- Print output as the total number of fair pairs.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void countPairs(string S, int n, int K)
{
map< char , long long int > mp;
for ( long long int i = 0; i < n; i++) {
mp[S[i]]++;
}
char xxx = 'a' ;
char yyy = 'A' ;
long long int output = 0;
for ( long long int i = 0; i < 26; i++) {
long long int l = mp[xxx];
long long int u = mp[yyy];
while (l > 0 && u > 0) {
output++;
l--;
u--;
}
while (K > 0 && u > 1) {
K--;
output++;
u -= 2;
}
while (K > 0 && l > 1) {
K--;
output++;
l -= 2;
}
xxx++;
yyy++;
}
cout << output << endl;
}
int main()
{
long long int N, K = 2;
string S = "xyyXzzYyxxZ" ;
N = S.length();
countPairs(S, N, K);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void countPairs(String S, int n, int K) {
Map<Character, Long> mp = new HashMap<>();
for ( long i = 0 ; i < n; i++) {
char ch = S.charAt(( int )i);
mp.put(ch, mp.getOrDefault(ch, 0L) + 1 );
}
char xxx = 'a' ;
char yyy = 'A' ;
long output = 0 ;
for ( long i = 0 ; i < 26 ; i++) {
long l = mp.getOrDefault(xxx, 0L);
long u = mp.getOrDefault(yyy, 0L);
while (l > 0 && u > 0 ) {
output++;
l--;
u--;
}
while (K > 0 && u > 1 ) {
K--;
output++;
u -= 2 ;
}
while (K > 0 && l > 1 ) {
K--;
output++;
l -= 2 ;
}
xxx++;
yyy++;
}
System.out.println(output);
}
public static void main(String[] args) {
int K = 2 ;
String S = "xyyXzzYyxxZ" ;
int N = S.length();
countPairs(S, N, K);
}
}
|
Python3
def countPairs(S: str , n: int , K: int ) - > None :
mp = {}
for i in range (n):
mp[S[i]] = mp.get(S[i], 0 ) + 1
xxx = 'a'
yyy = 'A'
output = 0
for i in range ( 26 ):
l = mp.get(xxx, 0 )
u = mp.get(yyy, 0 )
while l > 0 and u > 0 :
output + = 1
l - = 1
u - = 1
while K > 0 and u > 1 :
K - = 1
output + = 1
u - = 2
while K > 0 and l > 1 :
K - = 1
output + = 1
l - = 2
xxx = chr ( ord (xxx) + 1 )
yyy = chr ( ord (yyy) + 1 )
print (output)
if __name__ = = '__main__' :
N, K = 2 , 2
S = "xyyXzzYyxxZ"
countPairs(S, len (S), K)
|
C#
using System;
using System.Collections.Generic;
class MainClass {
public static void CountPairs( string S, int n, int K) {
Dictionary< char , long > mp = new Dictionary< char , long >();
for ( long i = 0; i < n; i++) {
char ch = S[( int )i];
if (mp.ContainsKey(ch)) {
mp[ch]++;
} else {
mp[ch] = 1;
}
}
char xxx = 'a' ;
char yyy = 'A' ;
long output = 0;
for ( long i = 0; i < 26; i++) {
long l = mp.ContainsKey(xxx) ? mp[xxx] : 0;
long u = mp.ContainsKey(yyy) ? mp[yyy] : 0;
while (l > 0 && u > 0) {
output++;
l--;
u--;
}
while (K > 0 && u > 1) {
K--;
output++;
u -= 2;
}
while (K > 0 && l > 1) {
K--;
output++;
l -= 2;
}
xxx++;
yyy++;
}
Console.WriteLine(output);
}
public static void Main( string [] args) {
int K = 2;
string S = "xyyXzzYyxxZ" ;
int N = S.Length;
CountPairs(S, N, K);
}
}
|
Javascript
function countPairs(S, N, K) {
let mp = new Map();
for (let i = 0; i < N; i++) {
let char = S.charAt(i);
if (mp.has(char)) {
mp.set(char, mp.get(char) + 1);
}
else {
mp.set(char, 1);
}
}
let xxx = 'a' ;
let yyy = 'A' ;
let output = 0;
for (let i = 0; i < 26; i++) {
let l = mp.get(xxx) || 0;
let u = mp.get(yyy) || 0;
while (l > 0 && u > 0) {
output++;
l--;
u--;
}
while (K > 0 && u > 1) {
K--;
output++;
u -= 2;
}
while (K > 0 && l > 1) {
K--;
output++;
l -= 2;
}
xxx = String.fromCharCode(xxx.charCodeAt(0) + 1);
yyy = String.fromCharCode(yyy.charCodeAt(0) + 1);
}
console.log(output);
}
let N = 0;
let K = 2;
let S = "xyyXzzYyxxZ" ;
N = S.length;
countPairs(S, N, K);
|
Time Complexity: O(n + log n)
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...