📌  相关文章
📜  使用给定的操作将字符串减少到最小长度

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

使用给定的操作将字符串减少到最小长度

给定一个由小写和大写字符组成的字符串str ,任务是找到在执行给定操作任意次数后字符串可以减少到的最小可能长度。在一次操作中,任意两个连续字符如果在不同情况下表示相同的字符,则可以删除,即“aA”“Cc”可以删除,但“cc”“EE”不能删除。
例子:

方法:

  • 创建一个堆栈来存储字符的字符串。
  • 对于从第一个字符开始的字符串的每个字符,如果堆栈为空,则将当前字符压入堆栈。
  • 否则将当前字符与堆栈顶部匹配,如果它们仅在大小写上有所不同,则从堆栈中弹出元素并继续。
  • 如果它们不相等,则将当前元素推入堆栈,并对字符串的其余部分重复上述步骤。
  • 最后堆栈的大小是所需的答案。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the minimum
// possible length str can be reduced
// to with the given operation
int minLength(string str, int len)
{
 
    // Stack to store the characters
    // of the given string
    stack s;
 
    // For every character of the string
    for (int i = 0; i < len; i++) {
 
        // If the stack is empty then push the
        // current character in the stack
        if (s.empty()) {
            s.push(str[i]);
        }
        else {
 
            // Get the top character
            char c = s.top();
 
            // If the top element is not equal
            // to the current element and it
            // only differs in the case
            if (c != str[i]
                && toupper(c) == toupper(str[i])) {
 
                // Pop the top element from stack
                s.pop();
            }
 
            // Else push the current element
            else {
                s.push(str[i]);
            }
        }
    }
 
    return s.size();
}
 
// Driver code
int main()
{
    string str = "ASbBsd";
    int len = str.length();
 
    cout << minLength(str, len);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
// Function to return the minimum
// possible length str can be reduced
// to with the given operation
static int minLength(String str, int len)
{
 
    // Stack to store the characters
    // of the given string
    Stack s = new Stack();
 
    // For every character of the string
    for (int i = 0; i < len; i++)
    {
 
        // If the stack is empty then push the
        // current character in the stack
        if (s.empty())
        {
            s.push(str.charAt(i));
        }
        else
        {
 
            // Get the top character
            char c = s.peek();
 
            // If the top element is not equal
            // to the current element and it
            // only differs in the case
            if (c != str.charAt(i) &&
                Character.toUpperCase(c) ==
                Character.toUpperCase((str.charAt(i))))
            {
 
                // Pop the top element from stack
                s.pop();
            }
 
            // Else push the current element
            else
            {
                s.push(str.charAt(i));
            }
        }
    }
    return s.size();
}
 
// Driver code
public static void main(String []args)
{
    String str = "ASbBsd";
    int len = str.length();
 
    System.out.println(minLength(str, len));
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation of the approach
 
# Function to return the minimum
# possible length str can be reduced
# to with the given operation
def minLength(string, l) :
 
    # Stack to store the characters
    # of the given string
    s = [];
 
    # For every character of the string
    for i in range(l) :
 
        # If the stack is empty then push the
        # current character in the stack
        if (len(s) == 0) :
            s.append(string[i]);
             
        else :
 
            # Get the top character
            c = s[-1];
             
            # If the top element is not equal
            # to the current element and it
            # only differs in the case
            if (c != string[i] and
                c.upper() == string[i].upper()) :
 
                # Pop the top element from stack
                s.pop();
 
            # Else push the current element
            else :
                s.append(string[i]);
 
    return len(s);
 
# Driver code
if __name__ == "__main__" :
 
    string = "ASbBsd";
    l = len(string);
 
    print(minLength(string, l));
 
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
 
class GFG
{
 
// Function to return the minimum
// possible length str can be reduced
// to with the given operation
static int minLength(String str, int len)
{
 
    // Stack to store the characters
    // of the given string
    Stack s = new Stack();
 
    // For every character of the string
    for (int i = 0; i < len; i++)
    {
 
        // If the stack is empty then push the
        // current character in the stack
        if (s.Count==0)
        {
            s.Push(str[i]);
        }
        else
        {
 
            // Get the top character
            char c = s.Peek();
 
            // If the top element is not equal
            // to the current element and it
            // only differs in the case
            if (c != str[i] &&
                char.ToUpper(c) ==
                char.ToUpper((str[i])))
            {
 
                // Pop the top element from stack
                s.Pop();
            }
 
            // Else push the current element
            else
            {
                s.Push(str[i]);
            }
        }
    }
    return s.Count;
}
 
// Driver code
public static void Main(String []args)
{
    String str = "ASbBsd";
    int len = str.Length;
 
    Console.WriteLine(minLength(str, len));
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
2

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