📌  相关文章
📜  通过删除仅出现一个字符最小化字符串的长度

📅  最后修改于: 2021-10-27 09:17:40             🧑  作者: Mango

给定一个只包含小写字母的字符串str ,任务是在执行以下操作后最小化字符串的长度:

  • 从此字符串删除所有出现的任何一个字符。

例子:

Input: str = "abccderccwq"
Output: 7
character 'c' will be deleted since it has maximum occurrence.

Input: str = "dddded"
Output: 1
character 'd' will be deleted

方法:

  1. 保持数组中每个字符的频率。
  2. 找出出现频率最高的字符。
  3. 最后,用该字符的频率减去原始字符串的长度以获得所需的答案。

下面是上述方法的实现:

C++
// C++ program to minimize the length of string by
// removing occurrence of only one character
#include 
using namespace std;
 
// Function to find the minimum length
int minimumLength(string s)
{
    int maxOcc = 0, n = s.length();
    int arr[26] = {0};
 
    // Count the frequency of each alphabet
    for (int i = 0; i < n; i++)
        arr[s[i] - 'a']++;
 
    // Find the alphabets with maximum frequency
    for (int i = 0; i < 26; i++)
        if (arr[i] > maxOcc)
            maxOcc = arr[i];
 
    // Subtract the frequency of character
    // from length of string
    return (n - maxOcc);
}
 
// Driver Code
int main()
{
    string str = "afddewqd";
    cout << minimumLength(str);
 
    return 0;
}


Java
// Java program to minimize the length of string
// by removing occurrence of only one character
import java.io.*;
 
class GFG {
    
 
// Function to find the minimum length
static int minimumLength(String s)
{
    int maxOcc = 0, n = s.length();
    int arr[] = new int[26];
 
    // Count the frequency of each alphabet
    for (int i = 0; i < n; i++)
        arr[s.charAt(i) - 'a']++;
 
    // Find the alphabets with maximum frequency
    for (int i = 0; i < 26; i++)
        if (arr[i] > maxOcc)
            maxOcc = arr[i];
 
    // Subtract the frequency of character
    // from length of string
    return (n - maxOcc);
}
 
// Driver Code
 
    public static void main (String[] args) {
    String str = "afddewqd";
    System.out.println( minimumLength(str));
    }
}
 
//This code is contributed by chandan_jnu...


Python 3
# Python 3 program to minimize the length of string
# by removing occurrence of only one character
 
# Function to find the minimum length
def minimumLength(s) :
 
    maxOcc = 0
    n = len(s)
    arr = [0]*26
 
    # Count the frequency of each alphabet
    for i in range(n) :
        arr[ord(s[i]) -ord('a')] += 1
 
    # Find the alphabets with maximum frequency
    for i in range(26) :
        if arr[i] > maxOcc :
            maxOcc = arr[i]
 
    # Subtract the frequency of character
    # from length of string
    return n - maxOcc
 
 
# Driver Code
if __name__ == "__main__" :
 
    str = "afddewqd"
    print(minimumLength(str))
 
 
# This code is contributed by ANKITRAI1


C#
// C# program to minimize the
// length of string by removing
// occurrence of only one character
using System;
 
class GFG
{
    // Function to find the minimum length
    static int minimumLength(String s)
    {
        int maxOcc = 0, n = s.Length;
        int []arr = new int[26];
 
        // Count the frequency of each alphabet
        for (int i = 0; i < n; i++)
            arr[s[i] - 'a']++;
 
        // Find the alphabets with
        // maximum frequency
        for (int i = 0; i < 26; i++)
            if (arr[i] > maxOcc)
                maxOcc = arr[i];
 
        // Subtract the frequency of character
        // from length of string
        return (n - maxOcc);
    }
 
    // Driver Code
    public static void Main (String[] args)
    {
        String str = "afddewqd";
        Console.WriteLine( minimumLength(str));
    }
}
 
// This code is contributed by
// PrinciRaj1992


Javascript


输出:
5

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程