Open In App

Minimum number of characters required to be added to a String such that all lowercase alphabets occurs as a subsequence in increasing order

Improve
Improve
Like Article
Like
Save
Share
Report

Given a string S consisting of N characters, the task is to find the minimum number of characters that must be added to S such that all lowercase alphabets occur as a subsequence in increasing order in S.

Examples:

Input: S = “axyabzaxd”
Output: 22
Explanation:
The characters from b to w (bcdefghijklmnopqrstuvw) whose count is 22, is missing in the given string S.
So, to make all the lower case english characters as a subsequence in it we have to add all these 22 characters. 
Therefore, the minimum characters to be added is 22.

Input: S = “abcdefghixyabzjklmnoaxpqrstd”
Output: 6

Naive Approach: The simplest approach to solve the given problem is to generate all possible subsequences of the given string S and check in which subsequence appending the minimum number of characters in the string gives the subsequence of all lowercase alphabets in increasing order. After checking for all the subsequences, print the minimum number of characters appended. 
Time Complexity: O(2N)
Auxiliary Space: O(1)

Efficient Approach: The above approach can also be optimized by the following observation:

  • Since all the lowercase characters must be present after appending characters i.e., string S must contain string T as “abcdefghijklmnopqrstuvwxyz” as a subsequence.
  • The minimum number of characters that must be appended in any order is to first find the Longest Common Subsequence(say L) of string S and string T as this gives the maximum number of characters in increasing order that we don’t need to add
  • And then print the value of (26 – L) as the resultant minimum characters required.

From the above observations, find the value of LCS of the string S and T and print the value of (26 – LCS) as the result.

Below is the implementation of the above approach:

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<vector<int> >& 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<vector<int> > dp(N + 1, vector<int>(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




<script>
// javascript program for the above approach
 
 // Function to find the LCS
  // of strings S and string T
  function LCS(S , N, T,
                 M , 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
  function minimumCharacter(S)
  {
 
    // String containing all the characters
    var T = "abcdefghijklmnopqrstuvwxyz";
 
    var N = S.length, M = T.length;
 
    // Stores the result of overlapping
    // subproblems
    var dp= Array(N+1).fill(0).map(x => Array(M+1).fill(0));
    // Return the minimum characters
    // required
    return (26 - LCS(S, N, T, M, dp));
  }
 
  // Driver Code
  var S = "abcdadc";
 
  document.write(minimumCharacter(S));
 
// This code is contributed by 29AjayKumar
</script>


Output: 

22

 

Time Complexity: O(26*N)
Auxiliary Space: O(1)



Last Updated : 09 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads