Minimum number of characters required to be added to a String such that all lowercase alphabets occurs as a subsequence in increasing order
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++
#include "bits/stdc++.h"
using namespace std;
int LCS(string& S, int N, string& T, int M,
vector<vector< int > >& dp)
{
if (N == 0 or M == 0)
return 0;
if (dp[N][M] != -1)
return dp[N][M];
if (S[N - 1] == T[M - 1]) {
return dp[N][M] = 1 + LCS(S, N - 1, T, M - 1, dp);
}
return dp[N][M] = max(LCS(S, N - 1, T, M, dp),
LCS(S, N, T, M - 1, dp));
}
int minimumCharacter(string& S)
{
string T = "abcdefghijklmnopqrstuvwxyz" ;
int N = S.length(), M = T.length();
vector<vector< int > > dp(N + 1, vector< int >(M + 1, -1));
return (26 - LCS(S, N, T, M, dp));
}
int main()
{
string S = "abcdadc" ;
cout << minimumCharacter(S);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int LCS(String S, int N, String T,
int M, int dp[][])
{
if (N == 0 || M == 0 )
return 0 ;
if (dp[N][M] != 0 )
return dp[N][M];
if (S.charAt(N - 1 )== T.charAt(M - 1 )) {
return dp[N][M] = 1 + LCS(S, N - 1 ,
T, M - 1 , dp);
}
return dp[N][M] = Math.max(
LCS(S, N - 1 , T, M, dp),
LCS(S, N, T, M - 1 , dp));
}
static int minimumCharacter(String S)
{
String T = "abcdefghijklmnopqrstuvwxyz" ;
int N = S.length(), M = T.length();
int dp[][]= new int [N+ 1 ][M+ 1 ];
return ( 26 - LCS(S, N, T, M, dp));
}
public static void main (String[] args) {
String S = "abcdadc" ;
System.out.println(minimumCharacter(S));
}
}
|
Python
import numpy as np
def LCS(S, N, T, M, dp) :
if (N = = 0 or M = = 0 ) :
return 0 ;
if (dp[N][M] ! = 0 ) :
return dp[N][M];
if (S[N - 1 ] = = T[M - 1 ]) :
dp[N][M] = 1 + LCS(S, N - 1 , T, M - 1 , dp);
return dp[N][M]
dp[N][M] = max ( LCS(S, N - 1 , T, M, dp), LCS(S, N, T, M - 1 , dp));
return dp[N][M]
def minimumCharacter(S) :
T = "abcdefghijklmnopqrstuvwxyz" ;
N = len (S); M = len (T);
dp = np.zeros((N + 1 , M + 1 ));
return ( 26 - LCS(S, N, T, M, dp));
if __name__ = = "__main__" :
S = "abcdadc" ;
print (minimumCharacter(S));
|
C#
using System;
public class GFG
{
static int LCS(String S, int N, String T,
int M, int [,]dp)
{
if (N == 0 || M == 0)
return 0;
if (dp[N,M] != 0)
return dp[N,M];
if (S[N - 1]== T[M - 1]) {
return dp[N,M] = 1 + LCS(S, N - 1,
T, M - 1, dp);
}
return dp[N,M] = Math.Max(
LCS(S, N - 1, T, M, dp),
LCS(S, N, T, M - 1, dp));
}
static int minimumchar(String S)
{
String T = "abcdefghijklmnopqrstuvwxyz" ;
int N = S.Length, M = T.Length;
int [,]dp= new int [N+1,M+1];
return (26 - LCS(S, N, T, M, dp));
}
public static void Main(String[] args) {
String S = "abcdadc" ;
Console.WriteLine(minimumchar(S));
}
}
|
Javascript
<script>
function LCS(S , N, T,
M , dp)
{
if (N == 0 || M == 0)
return 0;
if (dp[N][M] != 0)
return dp[N][M];
if (S.charAt(N - 1)== T.charAt(M - 1)) {
return dp[N][M] = 1 + LCS(S, N - 1,
T, M - 1, dp);
}
return dp[N][M] = Math.max(
LCS(S, N - 1, T, M, dp),
LCS(S, N, T, M - 1, dp));
}
function minimumCharacter(S)
{
var T = "abcdefghijklmnopqrstuvwxyz" ;
var N = S.length, M = T.length;
var dp= Array(N+1).fill(0).map(x => Array(M+1).fill(0));
return (26 - LCS(S, N, T, M, dp));
}
var S = "abcdadc" ;
document.write(minimumCharacter(S));
</script>
|
Time Complexity: O(26*N)
Auxiliary Space: O(1)
Last Updated :
09 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...