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

📅  最后修改于: 2021-10-25 11:18:33             🧑  作者: 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


Javascript


输出:
tz

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程