📌  相关文章
📜  除去K个字符后,按字典顺序在字符串上可能的最大字符串

📅  最后修改于: 2021-05-04 11:18:12             🧑  作者: Mango

给定一个仅包含小写字母的字符串S ,任务是找到从字典上最大的字符串,该字符串可以通过从给定字符串删除K个字符来获得。

例子:

方法:
想法是使用堆栈数据结构来解决该问题。请按照以下步骤解决问题:

  • 遍历字符串。
  • 对于每个字符,请检查它是否大于堆栈顶部的字符。如果确定为true,则在K> 0的情况下弹出堆栈的顶部元素。
  • 将字符插入堆栈。
  • 完整遍历字符串,如果K> 0 ,则除去堆栈中的前K个元素。
  • 最后,将字符存储在堆栈中作为答案。打印答案

下面是上述方法的实现:

C++
// C++ Program to implement the
// above approach
#include 
using namespace std;
 
string largestString(string num, int k)
{
    // final result string
    string ans = "";
 
    for (auto i : num) {
 
        // If the current char exceeds the
        // character at the top of the stack
        while (ans.length() && ans.back() < i
               && k > 0) {
 
            // Remove from the end of the string
            ans.pop_back();
 
            // Decrease k for the removal
            k--;
        }
 
        // Insert current character
        ans.push_back(i);
    }
 
    // Perform remaining K deletions
    // from the end of the string
    while (ans.length() and k--) {
        ans.pop_back();
    }
 
    // Return the string
    return ans;
}
 
// Driver Code
int main()
{
    string str = "zyxedcba";
    int k = 1;
 
    cout << largestString(str, k) << endl;
}


Java
// Java program to implement the
// above approach
class GFG{
 
static String largestString(String num, int k)
{
     
    // Final result String
    String ans = "";
 
    for(char i : num.toCharArray())
    {
         
        // If the current char exceeds the
        // character at the top of the stack
        while (ans.length() > 0 &&
               ans.charAt(ans.length() - 1) < i &&
                                          k > 0)
        {
             
            // Remove from the end of the String
            ans = ans.substring(0, ans.length() - 1);
 
            // Decrease k for the removal
            k--;
        }
 
        // Insert current character
        ans += i;
    }
 
    // Perform remaining K deletions
    // from the end of the String
    while (ans.length() > 0 && k-- > 0)
    {
        ans = ans.substring(0, ans.length() - 1);
    }
     
    // Return the String
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    String str = "zyxedcba";
    int k = 1;
 
    System.out.print(largestString(str, k) + "\n");
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to implement the
# above approach
def largestString(num, k):
     
    # Final result string
    ans = []
     
    for i in range(len(num)):
         
        # If the current char exceeds the
        # character at the top of the stack
        while(len(ans) and ans[-1] < num[i] and
                                 k > 0):
             
            # Remove from the end of the string
            ans.pop()
             
            # Decrease k for the removal
            k -= 1
         
        # Insert current character
        ans.append(num[i])
     
    # Perform remaining K deletions
    # from the end of the string
    while(len(ans) and k):
        k -= 1
        ans.pop()
     
    # Return the string
    return ans
     
# Driver code
str = "zyxedcba"
k = 1
 
print(*largestString(str, k), sep = "")
 
# This code is contributed by divyeshrabadiya07


C#
// C# program to implement the
// above approach
using System;
 
class GFG{
 
static String largestString(String num, int k)
{
     
    // Final result String
    String ans = "";
 
    foreach(char i in num.ToCharArray())
    {
         
        // If the current char exceeds the
        // character at the top of the stack
        while (ans.Length > 0 &&
           ans[ans.Length - 1] < i && k > 0)
        {
             
            // Remove from the end of the String
            ans = ans.Substring(0, ans.Length - 1);
 
            // Decrease k for the removal
            k--;
        }
 
        // Insert current character
        ans += i;
    }
 
    // Perform remaining K deletions
    // from the end of the String
    while (ans.Length > 0 && k-- > 0)
    {
        ans = ans.Substring(0, ans.Length - 1);
    }
     
    // Return the String
    return ans;
}
 
// Driver Code
public static void Main(String[] args)
{
    String str = "zyxedcba";
    int k = 1;
 
    Console.Write(largestString(str, k) + "\n");
}
}
 
// This code is contributed by 29AjayKumar


输出:
zyxedcb


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