📜  要删除的最少字母以使给定字母的所有出现连续

📅  最后修改于: 2021-09-02 07:05:07             🧑  作者: Mango

给定一个字符串str和一个字符K ,任务是找到应该从这个序列中删除的最少元素数,以便给定字符K 的所有出现都变得连续。

例子:

方法:这个想法是在给定字符K 的第一次出现和最后一次出现之间找到除K之外的字符数。为此,遵循以下步骤:

  1. 查找字符K的第一次出现。
  2. 查找最后一次出现的字符K
  3. 在索引之间迭代并找出除 K 之外的那些索引之间存在的字符数。这是必需的答案。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
 
// Function to find the minimum number of
// deletions required to make the occurrences
// of the given character K continuous
int noOfDeletions(string str, char k)
{
    int ans = 0, cnt = 0, pos = 0;
 
    // Find the first occurrence of the given letter
    while (pos < str.length() && str[pos] != k) {
        pos++;
    }
 
    int i = pos;
 
    // Iterate from the first occurrence
    // till the end of the sequence
    while (i < str.length()) {
 
        // Find the index from where the occurrence
        // of the character is not continuous
        while (i < str.length() && str[i] == k) {
            i = i + 1;
        }
 
        // Update the answer with the number of
        // elements between non-consecutive occurrences
        // of the given letter
        ans = ans + cnt;
        cnt = 0;
        while (i < str.length() && str[i] != k) {
            i = i + 1;
 
            // Update the count for all letters
            // which are not equal to the given letter
            cnt = cnt + 1;
        }
    }
 
    // Return the count
    return ans;
}
 
// Driver code
int main()
{
    string str1 = "ababababa";
    char k1 = 'a';
    // Calling the function
    cout << noOfDeletions(str1, k1) << endl;
 
    string str2 = "kprkkoinkopt";
    char k2 = 'k';
    // Calling the function
    cout << noOfDeletions(str2, k2) << endl;
}


Java
// Java implementation of the above approach
import java.util.*;
 
class GFG{
  
// Function to find the minimum number of
// deletions required to make the occurrences
// of the given character K continuous
static int noOfDeletions(String str, char k)
{
    int ans = 0, cnt = 0, pos = 0;
  
    // Find the first occurrence of the given letter
    while (pos < str.length() && str.charAt(pos) != k) {
        pos++;
    }
  
    int i = pos;
  
    // Iterate from the first occurrence
    // till the end of the sequence
    while (i < str.length()) {
  
        // Find the index from where the occurrence
        // of the character is not continuous
        while (i < str.length() && str.charAt(i) == k) {
            i = i + 1;
        }
  
        // Update the answer with the number of
        // elements between non-consecutive occurrences
        // of the given letter
        ans = ans + cnt;
        cnt = 0;
        while (i < str.length() && str.charAt(i) != k) {
            i = i + 1;
  
            // Update the count for all letters
            // which are not equal to the given letter
            cnt = cnt + 1;
        }
    }
  
    // Return the count
    return ans;
}
  
// Driver code
public static void main(String[] args)
{
    String str1 = "ababababa";
    char k1 = 'a';
 
    // Calling the function
    System.out.print(noOfDeletions(str1, k1) +"\n");
  
    String str2 = "kprkkoinkopt";
    char k2 = 'k';
 
    // Calling the function
    System.out.print(noOfDeletions(str2, k2) +"\n");
}
}
 
// This code is contributed by Princi Singh


Python3
# Python3 implementation of the above approach
 
# Function to find the minimum number of
# deletions required to make the occurrences
# of the given character K continuous
def noOfDeletions(string, k) :
 
    ans = 0; cnt = 0; pos = 0;
 
    # Find the first occurrence of the given letter
    while (pos < len(string) and string[pos] != k) :
        pos += 1;
 
    i = pos;
 
    # Iterate from the first occurrence
    # till the end of the sequence
    while (i < len(string)) :
 
        # Find the index from where the occurrence
        # of the character is not continuous
        while (i < len(string) and string[i] == k) :
            i = i + 1;
         
        # Update the answer with the number of
        # elements between non-consecutive occurrences
        # of the given letter
        ans = ans + cnt;
        cnt = 0;
        while (i < len(string) and string[i] != k) :
            i = i + 1;
 
            # Update the count for all letters
            # which are not equal to the given letter
            cnt = cnt + 1;
             
    # Return the count
    return ans;
 
# Driver code
if __name__ == "__main__" :
 
    str1 = "ababababa";
    k1 = 'a';
     
    # Calling the function
    print(noOfDeletions(str1, k1));
     
    str2 = "kprkkoinkopt";
    k2 = 'k';
     
    # Calling the function
    print(noOfDeletions(str2, k2));
     
# This code is contributed by AnkitRai01


C#
// C# implementation of the above approach
using System;
 
class GFG{
   
// Function to find the minimum number of
// deletions required to make the occurrences
// of the given character K continuous
static int noOfDeletions(String str, char k)
{
    int ans = 0, cnt = 0, pos = 0;
   
    // Find the first occurrence of the given letter
    while (pos < str.Length && str[pos] != k) {
        pos++;
    }
   
    int i = pos;
   
    // Iterate from the first occurrence
    // till the end of the sequence
    while (i < str.Length) {
   
        // Find the index from where the occurrence
        // of the character is not continuous
        while (i < str.Length && str[i] == k) {
            i = i + 1;
        }
   
        // Update the answer with the number of
        // elements between non-consecutive occurrences
        // of the given letter
        ans = ans + cnt;
        cnt = 0;
        while (i < str.Length && str[i] != k) {
            i = i + 1;
   
            // Update the count for all letters
            // which are not equal to the given letter
            cnt = cnt + 1;
        }
    }
   
    // Return the count
    return ans;
}
   
// Driver code
public static void Main(String[] args)
{
    String str1 = "ababababa";
    char k1 = 'a';
  
    // Calling the function
    Console.Write(noOfDeletions(str1, k1) +"\n");
   
    String str2 = "kprkkoinkopt";
    char k2 = 'k';
  
    // Calling the function
    Console.Write(noOfDeletions(str2, k2) +"\n");
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
4
5

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live