给定长度为N的字符串str ,任务是找到要删除的子字符串的最小数量,以使该字符串的所有其余字符相同。
注意:要删除的子字符串不得包含字符串最后一个剩余的字符。
例子:
Input: str = “ACBDAB”
Output: 2
Explanation:
Removing the substring { str[1], …, str[3] } modifies str to “AAB”
Removing the substring {str[2] } modifies str to “AA”
Since all characters of str are equal, the required output is 2.
Input: str = “ZBCDEFZ”
Output: 1
Explanation:
Removing the substring { str[1], …, str[5] } modifies str to “ZZ”
Since all characters of str are equal, the required output is 1.
方法:我们的想法是先删除该字符串的所有连续重复的字符和计数字符串的显着的字符频率。最后,除去该字符串的所有字符以外的字符具有最小频率。请按照以下步骤解决问题:
- 遍历字符串的所有字符,并删除字符串的所有连续重复的字符。
- 计数字符串的每个不同的字符的频率和减小第一和由1字符串的最后一个字符的频率。
- 最后,打印获得的最小频率。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to count minimum operations
// required to make all characters equal
// by repeatedly removing substring
void minOperationNeeded(string str)
{
// Remove consecutive duplicate
// characters from str
str = string(str.begin(),
unique(str.begin(), str.end()));
// Stores length of the string
int N = str.length();
// Stores frequency of each distinct
// characters of the string str
int res[256] = { 0 };
// Iterate over all the characters
// of the string str
for (int i = 0; i < N; ++i) {
// Update frequency of str[i]
res[str[i]] += 1;
}
// Decrementing the frequency
// of the string str[0]
res[str[0]] -= 1;
// Decrementing the frequency
// of the string str[N - 1]
res[str[N - 1]] -= 1;
// Stores the required count
int ans = INT_MAX;
// Iterate over all characters
// of the string str
for (int i = 0; i < N; ++i) {
// Update ans
ans = min(ans, res[str[i]]);
}
cout << (ans + 1) << endl;
}
// Driver Code
int main()
{
// Given string
string str = "ABCDABCDABCDA";
minOperationNeeded(str);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG {
// Function to count minimum operations
// required to make all characters equal
// by repeatedly removing subString
static void minOperationNeeded(char[] str)
{
// Remove consecutive duplicate
// characters from str
str = modstring(str);
// Stores length of the String
int N = str.length;
// Stores frequency of each distinct
// characters of the String str
int res[] = new int[256];
// Iterate over all the characters
// of the String str
for (int i = 0; i < N; ++i) {
// Update frequency of str[i]
res[str[i]] += 1;
}
// Decrementing the frequency
// of the String str[0]
res[str[0]] -= 1;
// Decrementing the frequency
// of the String str[N - 1]
res[str[N - 1]] -= 1;
// Stores the required count
int ans = Integer.MAX_VALUE;
// Iterate over all characters
// of the String str
for (int i = 0; i < N; ++i) {
// Update ans
ans = Math.min(ans, res[str[i]]);
}
System.out.print((ans + 1) + "\n");
}
private static char[] modstring(char[] str) {
String s = "";
boolean b = true;
for (int i = 1; i < str.length; ++i) {
if (str[i - 1] != str[i])
b = true;
if (b) {
s += str[i-1];
b = false;
}
}
return s.toCharArray();
}
// Driver Code
public static void main(String[] args)
{
// Given String
String str = "ABCDABCDABCDA";
minOperationNeeded(str.toCharArray());
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program to implement
# the above approach
import re, sys
# Function to count minimum operations
# required to make all characters equal
# by repeatedly removing substring
def minOperationNeeded(s):
# Remove consecutive duplicate
# characters from str
d = {}
str = re.sub(r"(.)\1 + ",'', s)
# Stores length of the string
N = len(str)
# Stores frequency of each distinct
# characters of the string str
res = [0 for i in range(256)]
# Iterate over all the characters
# of the string str
for i in range(N):
# Update frequency of str[i]
res[ord(str[i])] += 1
# Decrementing the frequency
# of the string str[0]
res[ord(str[0])] -= 1
# Decrementing the frequency
# of the ord(string ord(str[N - 1]
res[ord(str[N - 1])] -= 1
# Stores the required count
ans = sys.maxsize
# Iterate over all characters
# of the string str
for i in range(N):
# Update ans
ans = min(ans, res[ord(str[i])])
print ((ans + 1))
# Driver Code
if __name__ == '__main__':
# Given string
str = "ABCDABCDABCDA"
minOperationNeeded(str)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG{
// Function to count minimum operations
// required to make all characters equal
// by repeatedly removing subString
static void minOperationNeeded(char[] str)
{
// Remove consecutive duplicate
// characters from str
str = modstring(str);
// Stores length of the String
int N = str.Length;
// Stores frequency of each distinct
// characters of the String str
int[] res = new int[256];
// Iterate over all the characters
// of the String str
for (int i = 0; i < N; ++i)
{
// Update frequency of str[i]
res[str[i]] += 1;
}
// Decrementing the frequency
// of the String str[0]
res[str[0]] -= 1;
// Decrementing the frequency
// of the String str[N - 1]
res[str[N - 1]] -= 1;
// Stores the required count
int ans = Int32.MaxValue;
// Iterate over all characters
// of the String str
for (int i = 0; i < N; ++i)
{
// Update ans
ans = Math.Min(ans, res[str[i]]);
}
Console.WriteLine((ans + 1) + "\n");
}
private static char[] modstring(char[] str)
{
string s = "";
bool b = true;
for (int i = 1; i < str.Length; ++i)
{
if (str[i - 1] != str[i])
b = true;
if (b)
{
s += str[i - 1];
b = false;
}
}
return s.ToCharArray();
}
// Driver Code
public static void Main()
{
// Given String
string str = "ABCDABCDABCDA";
minOperationNeeded(str.ToCharArray());
}
}
// This code is contributed by code_hunt.
输出:
3
时间复杂度: O(N)
辅助空间: O(256)