Given string str of lower case alphabets, the task is to output the minimum number of conversions to be made such that all characters are repeated an odd number of times, if the task is not possible, then print -1. Any alphabet can be converted into any other lower-case alphabet.
Examples:
Input: str = “geeksforgeeks”
Output: 2
Explanation: Convert one ‘g’ to ‘e’ and convert one ‘k’ to ‘s’
Input: str = “geeks”
Output: 1
Explanation: Convert ‘e’ to any character that is not present in the string
Approach: The given problem can be solved using a hashmap. The idea is to count the number of alphabets repeating an even number of times and storing their frequencies in the hashmap. The below approach can be followed to solve the problem:
- Iterate the string and put all characters in the hashmap:
- If the character is already present in the hashmap then increase its frequency by 1
- Iterate the hashmap and count the number of characters with even frequency and store it in a variable count
- If the value of count is:
- Even, then return count / 2
- Odd and the size of hashmap is:
- Less than 26 then return count / 2 + 1
- Equal to 26 then return -1
C++
#include <bits/stdc++.h>
using namespace std;
int maxconv(string s)
{
unordered_map< char , int > map;
for ( auto x : s) {
map[x]++;
}
int count = 0;
for ( auto z : map) {
if (z.second % 2 == 0)
count++;
}
if (count % 2 == 0)
return count / 2;
else if (count % 2 != 0) {
if (map.size() < 26) {
return (count / 2) + 1;
}
else {
return -1;
}
}
}
int main()
{
string s = "geeksforgeeks" ;
cout << maxconv(s) << "\n" ;
}
|
Java
import java.util.*;
class GFG
{
static int maxconv(String s)
{
HashMap<Character, Integer> map
= new HashMap<Character, Integer>();
char [] strArray =s.toCharArray();
for ( char c : strArray) {
if (map.containsKey(c)) {
map.put(c, map.get(c) + 1 );
}
else {
map.put(c, 1 );
}
}
int count = 0 ;
for (Map.Entry entry : map.entrySet())
{
if (( int )entry.getValue() % 2 == 0 )
{
count++;
}
}
if ((count % 2 ) == 0 )
return count / 2 ;
else if (count % 2 != 0 ) {
if (map.size() < 26 ) {
return (count / 2 ) + 1 ;
}
else {
return - 1 ;
}
}
return - 1 ;
}
public static void main(String[] args)
{
String s = "geeksforgeeks" ;
System.out.println(maxconv(s));
}
}
|
Python3
def maxconv(s):
map = {}
for x in s:
if x in map :
map [x] + = 1
else :
map [x] = 1
count = 0
for z in map :
if ( map [z] % 2 = = 0 ):
count + = 1
if (count % 2 = = 0 ):
return count / / 2
elif (count % 2 ! = 0 ):
if ( len ( map ) < 26 ):
return (count / / 2 ) + 1
else :
return - 1
if __name__ = = "__main__" :
s = "geeksforgeeks"
print (maxconv(s))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int maxconv(String s)
{
Dictionary< char , int > map = new Dictionary< char , int >();
char [] strArray = s.ToCharArray();
foreach ( char c in strArray)
{
if (map.ContainsKey(c))
{
map = map + 1;
}
else
{
map = 1;
}
}
int count = 0;
foreach ( int entry in map.Values)
{
if (entry % 2 == 0)
{
count++;
}
}
if ((count % 2) == 0)
return count / 2;
else if (count % 2 != 0)
{
if (map.Count < 26)
{
return (count / 2) + 1;
}
else
{
return -1;
}
}
return -1;
}
public static void Main()
{
String s = "geeksforgeeks" ;
Console.Write(maxconv(s));
}
}
|
Javascript
<script>
function maxconv(s)
{
let map = new Map();
for (x of s) {
if (map.has(x)) {
map.set(x, map.get(x) + 1)
} else {
map.set(x, 1)
}
}
let count = 0;
for (z of map) {
if (z[1] % 2 == 0)
count++;
}
if (count % 2 == 0)
return Math.floor(count / 2);
else if (count % 2 != 0) {
if (map.length < 26) {
return Math.floor(count / 2) + 1;
}
else {
return -1;
}
}
}
let s = "geeksforgeeks" ;
document.write(maxconv(s))
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)