📌  相关文章
📜  从字符串中删除最少以将其减少为最多具有 2 个唯一字符的字符串

📅  最后修改于: 2022-05-13 01:57:08.966000             🧑  作者: Mango

从字符串中删除最少以将其减少为最多具有 2 个唯一字符的字符串

给定一个字符串S  包含小写英文字母。任务是找出需要删除的最少字符数,以便剩余的字符串最多包含 2 个唯一字符。
注意:最后的字符串可以有重复的字符。任务只是以最少的删除来减少字符串,使得结果字符串中最多可以有 2 个唯一字符。

例子:

方法:首先统计给定字符串中每个字符出现的次数,然后只选择出现次数最多的两个字符,即字符串中出现频率最高的两个字符。结果将是:

下面是上述方法的实现:

C++
// C++ implementation of the above approach
 
#include
using namespace std;
 
// Function to find the minimum deletions
int check(string s)
{
     
    int i, j;
    // Array to store the occurrences
    // of each characters
    int fr[26] = {0} ;
 
    // Length of the string
    int n = s.size() ;
    for(i = 0; i < n; i++)
    {
         
        // ASCII of the character
        char x = s[i] ;
         
        // Increasing the frequency for this character
        fr[x-'a'] += 1 ;
     
    }
 
 
    int minimum = INT_MAX;
 
    for(i = 0 ; i < 26; i++)
    {
        for( j = i + 1;j < 26; j++)
        {
 
            // Choosing two character
            int z = fr[i] + fr[j] ;
 
            // Finding the minimum deletion
            minimum = min(minimum, n - z) ;
        }
    }
 
    return minimum ;
}
 
/* Driver code */
int main()
{
    string s ="geeksforgeeks" ;
    cout << check(s) ;
}
 
// This code is contributed by ihritik


Java
// Java implementation of the above approach
 
public class GFG{
 
// Function to find the minimum deletions
static int check(String s)
{
     
    int i,j;
    // Array to store the occurrences
    // of each characters
    int fr[] = new int[26] ;
 
    // Length of the string
    int n = s.length() ;
    for(i = 0; i < n; i++)
    {
         
        // ASCII of the character
        char x = s.charAt(i) ;
         
        // Increasing the frequency for this character
        fr[x-'a'] += 1 ;
     
    }
 
 
    int minimum = Integer.MAX_VALUE;
 
    for(i = 0 ; i < 26; i++)
    {
        for( j = i + 1;j < 26; j++)
        {
 
            // Choosing two character
            int z = fr[i] + fr[j] ;
 
            // Finding the minimum deletion
            minimum = Math.min(minimum, n-z) ;
        }
    }
 
    return minimum ;
}
 
    /* Driver program to test above functions */
     public static void main(String []args){
             
        String s ="geeksforgeeks" ;
        System.out.println(check(s)) ;
 
        }
    // This code is contributed by ANKITRAI1
}


Python3
# Python3 implementation of the above approach
 
# Function to find the minimum deletions
def check(s):
 
    # Array to store the occurrences
    # of each characters
    fr =[0]*26
 
    # Length of the string
    n = len(s)
    for i in range(n):
 
        # ASCII of the character
        x = ord(s[i])
 
        # Increasing the frequency for this character
        fr[x-97] += 1
 
    minimum = 99999999999
 
    for i in range(26):
        for j in range(i + 1, 26):
 
            # Choosing two character
            z = fr[i] + fr[j]
 
            # Finding the minimum deletion
            minimum = min(minimum, n-z)
 
    return minimum
 
# Driver code
s ="geeksforgeeks"
print(check(s))


C#
// C# implementation of the above approach
  
using System;
public class GFG{
  
// Function to find the minimum deletions
static int check(string s)
{
      
    int i,j;
    // Array to store the occurrences
    // of each characters
    int[] fr = new int[26] ;
  
    // Length of the string
    int n = s.Length ;
    for(i = 0; i < n; i++)
    {
          
        // ASCII of the character
        char x = s[i] ;
          
        // Increasing the frequency for this character
        fr[x-'a'] += 1 ;
      
    }
  
  
    int minimum = int.MaxValue;
  
    for(i = 0 ; i < 26; i++)
    {
        for( j = i + 1;j < 26; j++)
        {
  
            // Choosing two character
            int z = fr[i] + fr[j] ;
  
            // Finding the minimum deletion
            minimum = Math.Min(minimum, n-z) ;
        }
    }
  
    return minimum ;
}
  
    /* Driver program to test above functions */
     public static void Main(){
              
        string s ="geeksforgeeks" ;
        Console.Write(check(s)) ;
  
        }
}


Javascript


输出:
7

时间复杂度:O(N),其中 N 是给定字符串的长度。