Longest subsequence with consecutive English alphabets
Given string S, the task is to find the length of the longest subsequence of the consecutive lowercase alphabets.
Examples:
Input: S = “acbdcfhg”
Output: 3
Explanation:
String “abc” is the longest subsequence of consecutive lowercase alphabets.
Therefore, print 3 as it is the length of the subsequence “abc”.
Input: S = “gabbsdcdggbe”
Output: 5
Naive Approach: The simplest approach is to generate all possible subsequences of the given string and if there exists any subsequence of the given string that has consecutive characters and is of maximum length then print that length.
Time Complexity: O(2N)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized by generating all possible consecutive subsequences of the given string starting from each lowercase alphabets and print the maximum among all the subsequence found. Follow the steps below to solve the problem:
- Initialize a variable, say ans, as 0 that stores the maximum length of the consecutive subsequence.
- For each character ch over the range [a, z] perform the following:
- Initialize a variable cnt as 0 that stores the length of a subsequence of consecutive characters starting from ch.
- Traverse the given string S and if the current character is ch then increment the cnt by 1 and update the current character ch to the next character by (ch + 1).
- Update ans = max(ans, cnt)
- After the above steps, print the value of ans as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findSubsequence(string S, char ch)
{
int N = S.length();
int ans = 0;
for ( int i = 0; i < N; i++) {
if (S[i] == ch) {
ans++;
ch++;
}
}
return ans;
}
int findMaxSubsequence(string S)
{
int ans = 0;
for ( char ch = 'a' ; ch <= 'z' ; ch++) {
ans = max(ans, findSubsequence(S, ch));
}
return ans;
}
int main()
{
string S = "abcabefghijk" ;
cout << findMaxSubsequence(S);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
import java.util.Arrays;
class GFG{
static int findSubsequence(String S, char ch)
{
int N = S.length();
int ans = 0 ;
for ( int i = 0 ; i < N; i++)
{
if (S.charAt(i) == ch)
{
ans++;
ch++;
}
}
return ans;
}
static int findMaxSubsequence(String S)
{
int ans = 0 ;
for ( char ch = 'a' ; ch <= 'z' ; ch++)
{
ans = Math.max(ans, findSubsequence(S, ch));
}
return ans;
}
public static void main(String[] args)
{
String S = "abcabefghijk" ;
System.out.print(findMaxSubsequence(S));
}
}
|
Python3
def findSubsequence(S, ch):
N = len (S)
ans = 0
for i in range (N):
if (S[i] = = ch):
ans + = 1
ch = chr ( ord (ch) + 1 )
return ans
def findMaxSubsequence(S):
ans = 0
for ch in range ( ord ( 'a' ), ord ( 'z' ) + 1 ):
ans = max (ans, findSubsequence(S, chr (ch)))
return ans
if __name__ = = '__main__' :
S = "abcabefghijk"
print (findMaxSubsequence(S))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int findSubsequence( string S, char ch)
{
int N = S.Length;
int ans = 0;
for ( int i = 0; i < N; i++)
{
if (S[i] == ch)
{
ans++;
ch++;
}
}
return ans;
}
static int findMaxSubsequence( string S)
{
int ans = 0;
for ( char ch = 'a' ; ch <= 'z' ; ch++)
{
ans = Math.Max(ans, findSubsequence(S, ch));
}
return ans;
}
public static void Main()
{
string S = "abcabefghijk" ;
Console.Write(findMaxSubsequence(S));
}
}
|
Javascript
<script>
function findSubsequence(S,ch)
{
let N = S.length;
let ans = 0;
for (let i = 0; i < N; i++)
{
if (S[i] == ch)
{
ans++;
ch=String.fromCharCode(ch.charCodeAt(0)+1);
}
}
return ans;
}
function findMaxSubsequence(S)
{
let ans = 0;
for (let ch = 'a' .charCodeAt(0); ch <= 'z' .charCodeAt(0); ch++)
{
ans = Math.max(ans, findSubsequence(S, String.fromCharCode(ch)));
}
return ans;
}
let S = "abcabefghijk" ;
document.write(findMaxSubsequence(S));
</script>
|
Time Complexity: O(26*N)
Auxiliary Space: O(1)
Last Updated :
13 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...