使所有字符的频率为奇数的最小转换次数
给定小写字母的字符串str ,任务是输出要进行的最小转换次数,以使所有字符重复奇数次,如果任务不可能,则打印 -1。任何字母都可以转换为任何其他小写字母。
例子:
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
方法:给定的问题可以使用哈希图来解决。这个想法是计算重复偶数次的字母的数量并将它们的频率存储在哈希图中。可以按照以下方法解决问题:
- 迭代字符串并将所有字符放入哈希图中:
- 如果字符已经存在于哈希图中,则将其频率增加 1
- 迭代hashmap并计算频率均匀的字符数并将其存储在变量计数中
- 如果count的值为:
- 偶数,然后返回count / 2
- 奇数,hashmap 的大小为:
- 小于 26 则返回count / 2 + 1
- 等于 26 然后返回 -1
C++
// C++ implementation for the above approach
#include
using namespace std;
// Function to return minimum
// number of conversions required
int maxconv(string s)
{
// Declare a hashmap
unordered_map map;
// Insert character into the map
for (auto x : s) {
// Increment the frequency of the
// character present in the string
map[x]++;
}
// Count to store number of even
// frequency elements
int count = 0;
// Loop to calculate characters
// with even frequency
for (auto z : map) {
// If frequency is even
if (z.second % 2 == 0)
// Increment the count
count++;
}
// If characters with even
// frequency are even
if (count % 2 == 0)
// return count / 2 as the answer
return count / 2;
// If characters with even
// frequency are even are odd
else if (count % 2 != 0) {
// If map contains less than
// 26 distinct characters
if (map.size() < 26) {
// Return count / 2 + 1
// as the answer
return (count / 2) + 1;
}
else {
// If map contains 26
// distinct characters
return -1;
}
}
}
int main()
{
// Initialize the string
string s = "geeksforgeeks";
// Call the function and
// print the output
cout << maxconv(s) << "\n";
}
Java
// Java code for the above approach
import java.util.*;
class GFG
{
// Function to return minimum
// number of conversions required
static int maxconv(String s)
{
// Declare a hashmap
HashMap map
= new HashMap();
// Converting given string to char array
char[] strArray =s.toCharArray();
// Insert character into the map
for (char c : strArray) {
if (map.containsKey(c)) {
// If char is present in charCountMap,
// incrementing it's count by 1
map.put(c, map.get(c) + 1);
}
else {
// If char is not present in charCountMap,
// putting this char to charCountMap with 1
// as it's value
map.put(c, 1);
}
}
// Count to store number of even
// frequency elements
int count = 0;
// Loop to calculate characters
// with even frequency
for (Map.Entry entry : map.entrySet())
{
if ((int)entry.getValue() % 2 == 0)
{
// Increment the count
count++;
}
}
// If characters with even
// frequency are even
if ((count % 2) == 0)
// return count / 2 as the answer
return count / 2;
// If characters with even
// frequency are even are odd
else if (count % 2 != 0) {
// If map contains less than
// 26 distinct characters
if (map.size() < 26) {
// Return count / 2 + 1
// as the answer
return (count / 2) + 1;
}
else {
// If map contains 26
// distinct characters
return -1;
}
}
return -1;
}
public static void main(String[] args)
{
// Initialize the string
String s = "geeksforgeeks";
// Call the function and
// print the output
System.out.println(maxconv(s));
}
}
// This code is contributed by Potta Lokesh
Python3
# python implementation for the above approach
# Function to return minimum
# number of conversions required
def maxconv(s):
# Declare a hashmap
map = {}
# Insert character into the map
for x in s:
# Increment the frequency of the
# character present in the string
if x in map:
map[x] += 1
else:
map[x] = 1
# Count to store number of even
# frequency elements
count = 0
# Loop to calculate characters
# with even frequency
for z in map:
# If frequency is even
if (map[z] % 2 == 0):
# Increment the count
count += 1
# If characters with even
# frequency are even
if (count % 2 == 0):
# return count / 2 as the answer
return count // 2
# If characters with even
# frequency are even are odd
elif (count % 2 != 0):
# If map contains less than
# 26 distinct characters
if (len(map) < 26):
# Return count / 2 + 1
# as the answer
return (count // 2) + 1
else:
# If map contains 26
# distinct characters
return -1
if __name__ == "__main__":
# Initialize the string
s = "geeksforgeeks"
# Call the function and
# print the output
print(maxconv(s))
# This code is contributed by rakeshsahni
C#
// C# code for the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to return minimum
// number of conversions required
static int maxconv(String s)
{
// Declare a hashmap
Dictionary map = new Dictionary();
// Converting given string to char array
char[] strArray = s.ToCharArray();
// Insert character into the map
foreach (char c in strArray)
{
if (map.ContainsKey(c))
{
// If char is present in charCountMap,
// incrementing it's count by 1
map = map + 1;
}
else
{
// If char is not present in charCountMap,
// putting this char to charCountMap with 1
// as it's value
map = 1;
}
}
// Count to store number of even
// frequency elements
int count = 0;
// Loop to calculate characters
// with even frequency
foreach (int entry in map.Values)
{
if (entry % 2 == 0)
{
// Increment the count
count++;
}
}
// If characters with even
// frequency are even
if ((count % 2) == 0)
// return count / 2 as the answer
return count / 2;
// If characters with even
// frequency are even are odd
else if (count % 2 != 0)
{
// If map contains less than
// 26 distinct characters
if (map.Count < 26)
{
// Return count / 2 + 1
// as the answer
return (count / 2) + 1;
}
else
{
// If map contains 26
// distinct characters
return -1;
}
}
return -1;
}
public static void Main()
{
// Initialize the string
String s = "geeksforgeeks";
// Call the function and
// print the output
Console.Write(maxconv(s));
}
}
// This code is contributed by gfgking
Javascript
输出
2
时间复杂度: O(N)
辅助空间: O(N)