给定一个由小写英文字母组成的字符串S ,任务是找到需要删除的最少字符数,以便可以重新排列字符串的字符以形成回文。
例子:
Input: S = “ababccca”
Output: 1
Explanation:
Remove the occurrence of ‘c’ from the string. Therefore, the modified string is “ababcca”, which can be rearranged to form the palindromic string “cababac”.
Therefore, only one removal is required.
Input: S = abcde
Output: 4
方法:根据观察,在回文字符串,最多一个字符可以出现奇数次,可以解决这个问题。因此,减少除其中一个以外的所有奇数字符的频率。
请按照以下步骤解决问题:
- 初始化一个大小为26的数组,用于存储S中每个字符的频率。
- 遍历字符串
- 更新频率数组中每个字符的频率。
- 计算具有奇数频率的字符数并将其存储在变量中,例如count 。
- 如果计数为1或0 ,则打印0作为答案。否则,打印count – 1作为答案。
下面是上述方法的实现:
C++
// C++ program for
// the above approach
#include
using namespace std;
// Function to find the number of deletions
// required such that characters of the
// string can be rearranged to form a palindrome
int minDeletions(string str)
{
// Stores frequency of characters
int fre[26];
memset(fre, 0, sizeof(fre));
int n = str.size();
cout<
Java
// Java program for the above approach
public class GFG
{
// Function to find the number of deletions
// required such that characters of the
// string can be rearranged to form a palindrome
static int minDeletions(String str)
{
// Stores frequency of characters
int fre[] = new int[26];
int n = str.length();
// Store the frequency of each
// character in frequency array
for (int i = 0; i < n; i++) {
fre[str.charAt(i) - 'a'] += 1;
}
int count = 0;
// Count number of characters
// with odd frequency
for (int i = 0; i < 26; i++) {
if (fre[i] % 2 == 1) {
count += 1;
}
}
// If count is 1 or 0, return 0
if (count == 0 || count == 1) {
return 0;
}
// Otherwise, return count - 1
else {
return count - 1;
}
}
// Driver Code
public static void main (String[] args)
{
String str = "ababbccca";
// Function call to find minimum
// number of deletions required
System.out.println(minDeletions(str)) ;
}
}
// This code is contributed by AnkThon
Python3
# Python3 program for
# the above approach
# Function to find the number of deletions
# required such that characters of the
# string can be rearranged to form a palindrome
def minDeletions(str):
# Stores frequency of characters
fre = [0]*26
# memset(fre, 0, sizeof(fre));
n = len(str)
# Store the frequency of each
# character in frequency array
for i in range(n):
fre[ord(str[i]) - ord('a')] += 1
count = 0
# Count number of characters
# with odd frequency
for i in range(26):
if (fre[i] % 2):
count += 1
# If count is 1 or 0, return 0
if (count == 0 or count == 1):
return 0
# Otherwise, return count - 1
else:
return count - 1
# Driver Code
if __name__ == '__main__':
str = "ababbccca"
# Function call to find minimum
# number of deletions required
print (minDeletions(str))
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to find the number of deletions
// required such that characters of the
// string can be rearranged to form a palindrome
static int minDeletions(string str)
{
// Stores frequency of characters
int []fre = new int[26];
int n = str.Length;
// Store the frequency of each
// character in frequency array
for (int i = 0; i < n; i++)
{
fre[str[i] - 'a'] += 1;
}
int count = 0;
// Count number of characters
// with odd frequency
for (int i = 0; i < 26; i++) {
if (fre[i] % 2 == 1) {
count += 1;
}
}
// If count is 1 or 0, return 0
if (count == 0 || count == 1) {
return 0;
}
// Otherwise, return count - 1
else {
return count - 1;
}
}
// Driver Code
public static void Main(string[] args)
{
string str = "ababbccca";
// Function call to find minimum
// number of deletions required
Console.WriteLine(minDeletions(str)) ;
}
}
// This code is contributed by AnkThon
Javascript
输出:
2
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live