📌  相关文章
📜  使字符串满足给定条件所需的最少操作

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

使字符串满足给定条件所需的最少操作

给定一个字符串str ,任务是用最少的给定操作使字符串以相同的字符开始和结束。在单个操作中,可以删除字符串的任何字符。请注意,结果字符串的长度必须大于1 ,然后打印-1是不可能的。
例子:

方法:如果字符串需要以字符ch开始和结束,那么最佳方法是删除第一次出现ch之前的所有字符和最后一次出现ch之后的所有字符。找出从'a''z'的每个可能的ch值需要删除的字符数,并选择删除操作次数最少的字符数。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
const int MAX = 26;
 
// Function to return the minimum
// operations required
int minOperation(string str, int len)
{
 
    // To store the first and the last
    // occurrence of all the characters
    int first[MAX], last[MAX];
 
    // Set the first and the last occurrence
    // of all the characters to -1
    for (int i = 0; i < MAX; i++) {
        first[i] = -1;
        last[i] = -1;
    }
 
    // Update the occurrences of the characters
    for (int i = 0; i < len; i++) {
 
        int index = (str[i] - 'a');
 
        // Only set the first occurrence if
        // it hasn't already been set
        if (first[index] == -1)
            first[index] = i;
 
        last[index] = i;
    }
 
    // To store the minimum operations
    int minOp = -1;
 
    for (int i = 0; i < MAX; i++) {
 
        // If the frequency of the current
        // character in the string
        // is less than 2
        if (first[i] == -1 || first[i] == last[i])
            continue;
 
        // Count of characters to be
        // removed so that the string
        // starts and ends at the
        // current character
        int cnt = len - (last[i] - first[i] + 1);
 
        if (minOp == -1 || cnt < minOp)
            minOp = cnt;
    }
 
    return minOp;
}
 
// Driver code
int main()
{
    string str = "abcda";
    int len = str.length();
 
    cout << minOperation(str, len);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
    final static int MAX = 26;
     
    // Function to return the minimum
    // operations required
    static int minOperation(String str, int len)
    {
     
        // To store the first and the last
        // occurrence of all the characters
        int first[] = new int[MAX];
        int last[] = new int[MAX];
     
        // Set the first and the last occurrence
        // of all the characters to -1
        for (int i = 0; i < MAX; i++)
        {
            first[i] = -1;
            last[i] = -1;
        }
     
        // Update the occurrences of the characters
        for (int i = 0; i < len; i++)
        {
            int index = (str.charAt(i) - 'a');
     
            // Only set the first occurrence if
            // it hasn't already been set
            if (first[index] == -1)
                first[index] = i;
     
            last[index] = i;
        }
     
        // To store the minimum operations
        int minOp = -1;
     
        for (int i = 0; i < MAX; i++)
        {
     
            // If the frequency of the current
            // character in the string
            // is less than 2
            if (first[i] == -1 ||
                first[i] == last[i])
                continue;
     
            // Count of characters to be
            // removed so that the string
            // starts and ends at the
            // current character
            int cnt = len - (last[i] - first[i] + 1);
     
            if (minOp == -1 || cnt < minOp)
                minOp = cnt;
        }
        return minOp;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        String str = "abcda";
        int len = str.length();
     
        System.out.println(minOperation(str, len));
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python implementation of the approach
MAX = 26;
 
# Function to return the minimum
# operations required
def minOperation(str, len):
 
    # To store the first and the last
    # occurrence of all the characters
    first, last = [0] * MAX, [0] * MAX;
 
    # Set the first and the last occurrence
    # of all the characters to -1
    for i in range(MAX):
        first[i] = -1;
        last[i] = -1;
 
    # Update the occurrences of the characters
    for i in range(len):
 
        index = (ord(str[i]) - ord('a'));
 
        # Only set the first occurrence if
        # it hasn't already been set
        if (first[index] == -1):
            first[index] = i;
 
        last[index] = i;
 
    # To store the minimum operations
    minOp = -1;
 
    for i in range(MAX):
 
        # If the frequency of the current
        # character in the string
        # is less than 2
        if (first[i] == -1 or first[i] == last[i]):
            continue;
 
        # Count of characters to be
        # removed so that the string
        # starts and ends at the
        # current character
        cnt = len - (last[i] - first[i] + 1);
 
        if (minOp == -1 or cnt < minOp):
            minOp = cnt;
    return minOp;
 
# Driver code
str = "abcda";
len = len(str);
 
print( minOperation(str, len));
 
# This code is contributed by 29AjayKumar


C#
// C# implementation of the approach
using System;
     
class GFG
{
    readonly static int MAX = 26;
     
    // Function to return the minimum
    // operations required
    static int minOperation(String str, int len)
    {
     
        // To store the first and the last
        // occurrence of all the characters
        int []first = new int[MAX];
        int []last = new int[MAX];
     
        // Set the first and the last occurrence
        // of all the characters to -1
        for (int i = 0; i < MAX; i++)
        {
            first[i] = -1;
            last[i] = -1;
        }
     
        // Update the occurrences of the characters
        for (int i = 0; i < len; i++)
        {
            int index = (str[i] - 'a');
     
            // Only set the first occurrence if
            // it hasn't already been set
            if (first[index] == -1)
                first[index] = i;
     
            last[index] = i;
        }
     
        // To store the minimum operations
        int minOp = -1;
     
        for (int i = 0; i < MAX; i++)
        {
     
            // If the frequency of the current
            // character in the string
            // is less than 2
            if (first[i] == -1 ||
                first[i] == last[i])
                continue;
     
            // Count of characters to be
            // removed so that the string
            // starts and ends at the
            // current character
            int cnt = len - (last[i] - first[i] + 1);
     
            if (minOp == -1 || cnt < minOp)
                minOp = cnt;
        }
        return minOp;
    }
     
    // Driver code
    public static void Main (String[] args)
    {
        String str = "abcda";
        int len = str.Length;
     
        Console.WriteLine(minOperation(str, len));
    }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
0