📌  相关文章
📜  最小移除要求,以使给定的字符串仅包含一对交替的字符

📅  最后修改于: 2021-04-17 18:21:38             🧑  作者: Mango

给定字符串S ,任务是找到所需字符的最小除去,以使字符串S仅包含两个交替的字符。

例子:

方法:可以通过生成所有可能的26 2对英文字母并在字符串S中找到交替出现的最大长度(例如len)的一对来解决该问题。然后,通过从N减去len来打印实现此功能所需的字符数。

请按照以下步骤解决给定的问题:

  1. 初始化一个变量,例如len,以存储一对字符交替出现的最大长度。
  2. 遍历每对可能的英文字母,并针对每对英文字母执行以下操作:
    • 遍历字符串S的字符,并找到长,说newlen从字符串两个字符交替出现的。
    • 检查len是否小于newlen 。如果发现为真,则更新len = newlen
  3. 最后,打印N – len作为必需的答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the maximum
// length of alternating occurrences
// of a pair of characters in a string s
int findLength(string s, char i, char j)
{
    // Stores the next character
    // for alternating sequence
    char required = i;
 
    // Stores the length of alternating
    // occurrences of a pair of characters
    int length = 0;
 
    // Traverse the given string
    for (char curr : s) {
 
        // If current character is same
        // as the required character
        if (curr == required) {
 
            // Increase length by 1
            length += 1;
 
            // Reset required character
            if (required == i)
                required = j;
            else
                required = i;
        }
    }
 
    // Return the length
    return length;
}
 
// Function to find minimum characters
// required to be deleted from S to
// obtain an alternating sequence
int minimumDeletions(string S)
{
 
    // Stores maximum length
    // of alternating sequence
    // of two characters
    int len = 0;
 
    // Stores length of the string
    int n = S.length();
 
    // Generate every pair
    // of English alphabets
    for (char i = 'a'; i <= 'z'; i++) {
        for (char j = i + 1; j <= 'z'; j++) {
 
            // Function call to find length
            // of alternating sequence for
            // currrent pair of characters
            int newLen = findLength(S, i, j);
 
            // Update len to store the maximum
            // of len and newLen in len
            len = max(len, newLen);
        }
    }
 
    // Return n - len as the final result
    return n - len;
}
 
// Driver Code
int main()
{
    // Given Input
    string S = "adebbeeaebd";
 
    // Function call to find minimum
    // characters required to be removed
    // from S to make it an alternating
    // sequence of a pair of characters
    cout << minimumDeletions(S);
    return 0;
}


Java
// Java program for the above approach
class GFG{
     
// Function to find the maximum
// length of alternating occurrences
// of a pair of characters in a string s
static int findLength(String s, char i, char j)
{
     
    // Stores the next character
    // for alternating sequence
    char required = i;
 
    // Stores the length of alternating
    // occurrences of a pair of characters
    int length = 0;
 
    // Traverse the given string
    for(int k = 0; k < s.length(); k++)
    {
        char curr = s.charAt(k);
         
        // If current character is same
        // as the required character
        if (curr == required)
        {
             
            // Increase length by 1
            length += 1;
 
            // Reset required character
            if (required == i)
                required = j;
            else
                required = i;
        }
    }
 
    // Return the length
    return length;
}
 
// Function to find minimum characters
// required to be deleted from S to
// obtain an alternating sequence
static int minimumDeletions(String S)
{
     
    // Stores maximum length
    // of alternating sequence
    // of two characters
    int len = 0;
 
    // Stores length of the string
    int n = S.length();
 
    // Generate every pair
    // of English alphabets
    for(int i = 0; i < 26; i++)
    {
        for(int j = i + 1; j < 26; j++)
        {
             
            // Function call to find length
            // of alternating sequence for
            // currrent pair of characters
            int newLen = findLength(S, (char)(i + 97),
                                       (char)(j + 97));
 
            // Update len to store the maximum
            // of len and newLen in len
            len = Math.max(len, newLen);
        }
    }
 
    // Return n - len as the final result
    return n - len;
}
 
// Driver Code
public static void main (String[] args)
{
     
    // Given Input
    String S = "adebbeeaebd";
 
    // Function call to find minimum
    // characters required to be removed
    // from S to make it an alternating
    // sequence of a pair of characters
    System.out.print(minimumDeletions(S));
}
}
 
// This code is contributed by AnkThon


Python3
# Python3 program for the above approach
 
# Function to find the maximum
# length of alternating occurrences
# of a pair of characters in a string s
def findLength(s, i, j):
     
    # Stores the next character
    # for alternating sequence
    required = i
 
    # Stores the length of alternating
    # occurrences of a pair of characters
    length = 0
 
    # Traverse the given string
    for curr in s:
         
        # If current character is same
        # as the required character
        if (curr == required):
             
            # Increase length by 1
            length += 1
 
            # Reset required character
            if (required == i):
                required = j
            else:
                required = i
 
    # Return the length
    return length
 
# Function to find minimum characters
# required to be deleted from S to
# obtain an alternating sequence
def minimumDeletions(S):
     
    # Stores maximum length
    # of alternating sequence
    # of two characters
    len1 = 0
 
    # Stores length of the string
    n = len(S)
 
    # Generate every pair
    # of English alphabets
    for i in range(0, 26, 1):
        for j in range(i + 1, 26, 1):
             
            # Function call to find length
            # of alternating sequence for
            # currrent pair of characters
            newLen = findLength(S, chr(i + 97),
                                   chr(j + 97))
 
            # Update len to store the maximum
            # of len and newLen in len
            len1 = max(len1, newLen)
 
    # Return n - len as the final result
    return n - len1
 
# Driver Code
if __name__ == '__main__':
     
    # Given Input
    S = "adebbeeaebd"
 
    # Function call to find minimum
    # characters required to be removed
    # from S to make it an alternating
    # sequence of a pair of characters
    print(minimumDeletions(S))
 
# This code is contributed by ipg2016107


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
     
// Function to find the maximum
// length of alternating occurrences
// of a pair of characters in a string s
static int findLength(string s, char i, char j)
{
     
    // Stores the next character
    // for alternating sequence
    char required = i;
 
    // Stores the length of alternating
    // occurrences of a pair of characters
    int length = 0;
 
    // Traverse the given string
    for(int k = 0; k < s.Length; k++)
    {
        char curr = s[k];
         
        // If current character is same
        // as the required character
        if (curr == required)
        {
             
            // Increase length by 1
            length += 1;
 
            // Reset required character
            if (required == i)
                required = j;
            else
                required = i;
        }
    }
 
    // Return the length
    return length;
}
 
// Function to find minimum characters
// required to be deleted from S to
// obtain an alternating sequence
static int minimumDeletions(string S)
{
     
    // Stores maximum length
    // of alternating sequence
    // of two characters
    int len = 0;
 
    // Stores length of the string
    int n = S.Length;
 
    // Generate every pair
    // of English alphabets
    for(int i = 0; i < 26; i++)
    {
        for(int j = i + 1; j < 26; j++)
        {
             
            // Function call to find length
            // of alternating sequence for
            // currrent pair of characters
            int newLen = findLength(S, (char)(i + 97),
                                       (char)(j + 97));
 
            // Update len to store the maximum
            // of len and newLen in len
            len = Math.Max(len, newLen);
        }
    }
 
    // Return n - len as the final result
    return n - len;
}
 
// Driver Code
public static void Main(string[] args)
{
     
    // Given Input
    string S = "adebbeeaebd";
 
    // Function call to find minimum
    // characters required to be removed
    // from S to make it an alternating
    // sequence of a pair of characters
    Console.WriteLine(minimumDeletions(S));
}
}
 
// This code is contributed by avijitmondal1998


输出:
7

时间复杂度: O(N)
辅助空间: O(1)