📌  相关文章
📜  需要添加到字符串的最小字符数,以便所有小写字母作为子序列以递增顺序出现

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

需要添加到字符串的最小字符数,以便所有小写字母作为子序列以递增顺序出现

给定一个由N个字符组成的字符串S ,任务是找到必须添加到S中的最小字符数,以使所有小写字母在S中按升序出现。

例子:

朴素方法:解决给定问题的最简单方法是生成给定字符串S的所有可能子序列,并检查在哪个子序列中附加字符串中的最小字符数以升序给出所有小写字母的子序列。检查所有子序列后,打印附加的最小字符数。
时间复杂度: O(2 N )
辅助空间: O(1)

有效的方法:上述方法也可以通过以下观察进行优化:

  • 由于在附加字符之后必须存在所有小写字符,即字符串S必须包含字符串T作为“abcdefghijklmnopqrstuvwxyz”作为子序列。
  • 必须以任何顺序附加的最小字符数是首先找到字符串S和字符串T的最长公共子序列(比如L ),因为这给出了我们不需要添加的按递增顺序的最大字符数
  • 然后打印(26 – L)的值作为所需的最小字符数。

从以上观察中,找到字符串S 和 T 的 LCS 值,并打印(26 – LCS)的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include "bits/stdc++.h"
using namespace std;
 
// Function to find the LCS
// of strings S and string T
int LCS(string& S, int N, string& T, int M,
        vector >& dp)
{
 
    // Base Case
    if (N == 0 or M == 0)
        return 0;
 
    // Already Calculated State
    if (dp[N][M] != -1)
        return dp[N][M];
 
    // If the characters are the same
    if (S[N - 1] == T[M - 1]) {
        return dp[N][M] = 1 + LCS(S, N - 1, T, M - 1, dp);
    }
 
    // Otherwise
    return dp[N][M] = max(LCS(S, N - 1, T, M, dp),
                          LCS(S, N, T, M - 1, dp));
}
 
// Function to find the minimum number of
// characters that needs to be appended
// in the string to get all lowercase
// alphabets as a subsequences
int minimumCharacter(string& S)
{
 
    // String containing all the characters
    string T = "abcdefghijklmnopqrstuvwxyz";
 
    int N = S.length(), M = T.length();
 
    // Stores the result of overlapping
    // subproblems
    vector > dp(N + 1, vector(M + 1, -1));
 
    // Return the minimum characters
    // required
    return (26 - LCS(S, N, T, M, dp));
}
 
// Driver Code
int main()
{
 
    string S = "abcdadc";
    cout << minimumCharacter(S);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG
{
 
  // Function to find the LCS
  // of strings S and string T
  static int LCS(String S, int N, String T,
                 int M, int dp[][])
  {
 
    // Base Case
    if (N == 0 || M == 0)
      return 0;
 
    // Already Calculated State
    if (dp[N][M] != 0)
      return dp[N][M];
 
    // If the characters are the same
    if (S.charAt(N - 1)== T.charAt(M - 1)) {
      return dp[N][M] = 1 + LCS(S, N - 1,
                                T, M - 1, dp);
    }
 
    // Otherwise
    return dp[N][M] = Math.max(
      LCS(S, N - 1, T, M, dp),
      LCS(S, N, T, M - 1, dp));
  }
 
  // Function to find the minimum number of
  // characters that needs to be appended
  // in the string to get all lowercase
  // alphabets as a subsequences
  static int minimumCharacter(String S)
  {
 
    // String containing all the characters
    String T = "abcdefghijklmnopqrstuvwxyz";
 
    int N = S.length(), M = T.length();
 
    // Stores the result of overlapping
    // subproblems
    int dp[][]= new int[N+1][M+1];
    // Return the minimum characters
    // required
    return (26 - LCS(S, N, T, M, dp));
  }
 
  // Driver Code
  public static void main (String[] args) {
    String S = "abcdadc";
 
    System.out.println(minimumCharacter(S));
  }
}
 
// This code is contributed by Potta Lokesh


Python
# Python3 program for the above approach
import numpy as np
 
# Function to find the LCS
# of strings S and string T
def LCS(S, N, T, M, dp) :
 
    # Base Case
    if (N == 0 or M == 0) :
        return 0;
 
    # Already Calculated State
    if (dp[N][M] != 0) :
        return dp[N][M];
 
    # If the characters are the same
    if (S[N - 1] == T[M - 1]) :
        dp[N][M] = 1 + LCS(S, N - 1, T, M - 1, dp);
        return dp[N][M]
     
    # Otherwise
    dp[N][M] = max( LCS(S, N - 1, T, M, dp), LCS(S, N, T, M - 1, dp));
     
    return dp[N][M]
 
 
# Function to find the minimum number of
# characters that needs to be appended
# in the string to get all lowercase
# alphabets as a subsequences
def minimumCharacter(S) :
 
    # String containing all the characters
    T = "abcdefghijklmnopqrstuvwxyz";
 
    N = len(S); M = len(T);
 
    # Stores the result of overlapping
    # subproblems
    dp = np.zeros((N + 1, M + 1));
 
    # Return the minimum characters
    # required
    return (26 - LCS(S, N, T, M, dp));
 
 
# Driver Code
if __name__ == "__main__" :
 
    S = "abcdadc";
    print(minimumCharacter(S));
 
    # This code is contributed by AnkThon


C#
// C# program for the above approach
 
 
using System;
 
public class GFG
{
 
  // Function to find the LCS
  // of strings S and string T
  static int LCS(String S, int N, String T,
                 int M, int [,]dp)
  {
 
    // Base Case
    if (N == 0 || M == 0)
      return 0;
 
    // Already Calculated State
    if (dp[N,M] != 0)
      return dp[N,M];
 
    // If the characters are the same
    if (S[N - 1]== T[M - 1]) {
      return dp[N,M] = 1 + LCS(S, N - 1,
                                T, M - 1, dp);
    }
 
    // Otherwise
    return dp[N,M] = Math.Max(
      LCS(S, N - 1, T, M, dp),
      LCS(S, N, T, M - 1, dp));
  }
 
  // Function to find the minimum number of
  // characters that needs to be appended
  // in the string to get all lowercase
  // alphabets as a subsequences
  static int minimumchar(String S)
  {
 
    // String containing all the characters
    String T = "abcdefghijklmnopqrstuvwxyz";
 
    int N = S.Length, M = T.Length;
 
    // Stores the result of overlapping
    // subproblems
    int [,]dp= new int[N+1,M+1];
    // Return the minimum characters
    // required
    return (26 - LCS(S, N, T, M, dp));
  }
 
  // Driver Code
  public static void Main(String[] args) {
    String S = "abcdadc";
 
    Console.WriteLine(minimumchar(S));
  }
}
 
 
 
// This code is contributed by 29AjayKumar


Javascript


输出:
22

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