📌  相关文章
📜  删除K个字符后,按字典顺序获得的最大字符串

📅  最后修改于: 2021-04-22 06:13:19             🧑  作者: Mango

鉴于长度为N字符串str和一个整数K,任务是通过从字符串删除ķ要返回的字符在字典顺序最大的字符串。

例子:

天真的方法:这个想法是找到给定字符串长度N-K的所有子序列。将这些子序列存储在列表中。将有n C m个这样的序列。完成上述步骤后,按字母顺序打印列表中存储的最大字符串。

时间复杂度: O(2 NK )

高效方法:想法是使用 双端队列。步骤如下:

  1. 将字符串的所有字符存储在双端队列中。
  2. 遍历定的字符串,在字符串保持从双端队列弹出的字符每一个字符,如果它小于存储在双端队列的最后一个字符。执行此操作,直到K不为零。
  3. 现在,在完成上述操作之后,将当前字符插入双端队列。
  4. 完成上述操作后,由存储在双端队列中的字符形成的字符串就是结果字符串。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the largest
// string after deleting k characters
string largestString(int n, int k, string s)
{
     
    // Deque dq used to find the
    // largest string in dictionary
    // after deleting k characters
    deque deq;
 
    // Iterate till the length
    // of the string
    for(int i = 0; i < n; ++i)
    {
         
        // Condition for popping
        // characters from deque
        while (deq.size() > 0 &&
               deq.back() < s[i] &&
                        k > 0)
        {
            deq.pop_front();
            k--;
        }
 
        deq.push_back(s[i]);
    }
 
    // To store the resultant string
    string st = "";
 
    // To form resultant string
    for(char c : deq)
        st = st + c;
 
    // Return the resultant string
    return st;
}
 
// Driver code   
int main()
{
    int n = 4;
    int k = 2;
 
    // Given String
    string sc = "ritz";
 
    // Function call
    string result = largestString(n, k, sc);
 
    // Print the answer
    cout << result << endl;
         
    return 0;
}
 
// This code is contributed by divyeshrabadiya07


Java
// Java program for the above approach
 
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;
import java.io.IOException;
 
public class GFG {
 
    // Function to find the largest
    // string after deleting k characters
    public static String
    largestString(int n, int k, String sc)
    {
        char[] s = sc.toCharArray();
 
        // Deque dq used to find the
        // largest string in dictionary
        // after deleting k characters
        Deque deq
            = new ArrayDeque<>();
 
        // Iterate till the length
        // of the string
        for (int i = 0; i < n; ++i) {
 
            // Condition for popping
            // characters from deque
            while (deq.size() > 0
                   && deq.getLast() < s[i]
                   && k > 0) {
                deq.pollLast();
                k--;
            }
 
            deq.add(s[i]);
        }
 
        // To store the resultant string
        String st = "";
 
        // To form resultant string
        for (char c : deq)
            st = st + Character.toString(c);
 
        // Return the resultant string
        return st;
    }
 
    // Driver Code
    public static void main(String[] args)
        throws IOException
    {
        int n = 4;
        int k = 2;
 
        // Given String
        String sc = "ritz";
 
        // Function call
        String result = largestString(n, k, sc);
 
        // Print the answer
        System.out.println(result);
    }
}


Python3
# Python3 program for the above approach
from collections import deque
 
# Function to find the largest
# string after deleting k characters
def largestString(n, k, sc):
     
    s = [i for i in sc]
 
    # Deque dq used to find the
    # largest string in dictionary
    # after deleting k characters
    deq = deque()
 
    # Iterate till the length
    # of the string
    for i in range(n):
 
        # Condition for popping
        # characters from deque
        while (len(deq) > 0 and
                deq[-1] < s[i] and
                      k > 0):
            deq.popleft()
            k -= 1
 
        deq.append(s[i])
 
    # To store the resultant string
    st = ""
 
    # To form resultant string
    for c in deq:
        st = st + c
 
    # Return the resultant string
    return st
 
# Driver Code
if __name__ == '__main__':
     
    n = 4
    k = 2
 
    # Given String
    sc = "ritz"
 
    # Function call
    result = largestString(n, k, sc)
 
    # Print the answer
    print(result)
 
# This code is contributed by mohit kumar 29


输出:
tz




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