Longest subsequence with consecutive English alphabets
Last Updated :
13 Aug, 2021
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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the length of subsequence
// starting with character ch
int findSubsequence(string S, char ch)
{
// Length of the string
int N = S.length();
// Stores the maximum length
int ans = 0;
// Traverse the given string
for (int i = 0; i < N; i++) {
// If s[i] is required
// character ch
if (S[i] == ch) {
// Increment ans by 1
ans++;
// Increment character ch
ch++;
}
}
// Return the current maximum
// length with character ch
return ans;
}
// Function to find the maximum length
// of subsequence of consecutive
// characters
int findMaxSubsequence(string S)
{
// Stores the maximum length of
// consecutive characters
int ans = 0;
for (char ch = 'a'; ch <= 'z'; ch++) {
// Update ans
ans = max(ans, findSubsequence(S, ch));
}
// Return the maximum length of
// subsequence
return ans;
}
// Driver Code
int main()
{
// Input
string S = "abcabefghijk";
// Function Call
cout << findMaxSubsequence(S);
return 0;
}
Java
// C# program for the above approach
import java.io.*;
import java.util.*;
import java.util.Arrays;
class GFG{
// Function to find the length of subsequence
// starting with character ch
static int findSubsequence(String S, char ch)
{
// Length of the string
int N = S.length();
// Stores the maximum length
int ans = 0;
// Traverse the given string
for(int i = 0; i < N; i++)
{
// If s[i] is required
// character ch
if(S.charAt(i) == ch)
{
// Increment ans by 1
ans++;
// Increment character ch
ch++;
}
}
// Return the current maximum
// length with character ch
return ans;
}
// Function to find the maximum length
// of subsequence of consecutive
// characters
static int findMaxSubsequence(String S)
{
// Stores the maximum length of
// consecutive characters
int ans = 0;
for(char ch = 'a'; ch <= 'z'; ch++)
{
// Update ans
ans = Math.max(ans, findSubsequence(S, ch));
}
// Return the maximum length of
// subsequence
return ans;
}
// Driver Code
public static void main(String[] args)
{
// Input
String S = "abcabefghijk";
// Function Call
System.out.print(findMaxSubsequence(S));
}
}
// This code is contributed by shivanisinghss2110
Python3
# Python3 program for the above approach
# Function to find the length of subsequence
# starting with character ch
def findSubsequence(S, ch):
# Length of the string
N = len(S)
# Stores the maximum length
ans = 0
# Traverse the given string
for i in range(N):
# If s[i] is required
# character ch
if (S[i] == ch):
# Increment ans by 1
ans += 1
# Increment character ch
ch=chr(ord(ch) + 1)
# Return the current maximum
# length with character ch
return ans
# Function to find the maximum length
# of subsequence of consecutive
# characters
def findMaxSubsequence(S):
#Stores the maximum length of
# consecutive characters
ans = 0
for ch in range(ord('a'),ord('z') + 1):
# Update ans
ans = max(ans, findSubsequence(S, chr(ch)))
# Return the maximum length of
# subsequence
return ans
# Driver Code
if __name__ == '__main__':
# Input
S = "abcabefghijk"
# Function Call
print (findMaxSubsequence(S))
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the length of subsequence
// starting with character ch
static int findSubsequence(string S, char ch)
{
// Length of the string
int N = S.Length;
// Stores the maximum length
int ans = 0;
// Traverse the given string
for(int i = 0; i < N; i++)
{
// If s[i] is required
// character ch
if (S[i] == ch)
{
// Increment ans by 1
ans++;
// Increment character ch
ch++;
}
}
// Return the current maximum
// length with character ch
return ans;
}
// Function to find the maximum length
// of subsequence of consecutive
// characters
static int findMaxSubsequence(string S)
{
// Stores the maximum length of
// consecutive characters
int ans = 0;
for(char ch = 'a'; ch <= 'z'; ch++)
{
// Update ans
ans = Math.Max(ans, findSubsequence(S, ch));
}
// Return the maximum length of
// subsequence
return ans;
}
// Driver Code
public static void Main()
{
// Input
string S = "abcabefghijk";
// Function Call
Console.Write(findMaxSubsequence(S));
}
}
// This code is contributed by SURENDRA_GANGWAR
JavaScript
<script>
// Javascript program for the above approach
// Function to find the length of subsequence
// starting with character ch
function findSubsequence(S,ch)
{
// Length of the string
let N = S.length;
// Stores the maximum length
let ans = 0;
// Traverse the given string
for(let i = 0; i < N; i++)
{
// If s[i] is required
// character ch
if (S[i] == ch)
{
// Increment ans by 1
ans++;
// Increment character ch
ch=String.fromCharCode(ch.charCodeAt(0)+1);
}
}
// Return the current maximum
// length with character ch
return ans;
}
// Function to find the maximum length
// of subsequence of consecutive
// characters
function findMaxSubsequence(S)
{
// Stores the maximum length of
// consecutive characters
let ans = 0;
for(let ch = 'a'.charCodeAt(0); ch <= 'z'.charCodeAt(0); ch++)
{
// Update ans
ans = Math.max(ans, findSubsequence(S, String.fromCharCode(ch)));
}
// Return the maximum length of
// subsequence
return ans;
}
// Driver Code
let S = "abcabefghijk";
// Function Call
document.write(findMaxSubsequence(S));
// This code is contributed by patel2127
</script>
Time Complexity: O(26*N)
Auxiliary Space: O(1)
Similar Reads
Length of the longest substring with consecutive characters Given string str of lowercase alphabets, the task is to find the length of the longest substring of characters in alphabetical order i.e. string "dfabck" will return 3. Note that the alphabetical order here is considered circular i.e. a, b, c, d, e, ..., x, y, z, a, b, c, .... Examples: Input: str =
7 min read
Longest Increasing consecutive subsequence Given N elements, write a program that prints the length of the longest increasing consecutive subsequence. Examples: Input : a[] = {3, 10, 3, 11, 4, 5, 6, 7, 8, 12} Output : 6 Explanation: 3, 4, 5, 6, 7, 8 is the longest increasing subsequence whose adjacent element differs by one. Input : a[] = {6
10 min read
Longest Consecutive Subsequence Given an array of integers, the task is to find the length of the longest subsequence such that elements in the subsequence are consecutive integers, the consecutive numbers can be in any order. Examples: Input: arr[] = [2, 6, 1, 9, 4, 5, 3]Output: 6Explanation: The consecutive numbers here are from
9 min read
Longest Increasing consecutive subsequence | Set-2 Given an array arr[] of N elements, the task is to find the length of the longest increasing subsequence whose adjacent element difference is one. Examples: Input: arr[] = {3, 10, 3, 11, 4, 5, 6, 7, 8, 12} Output: 6 Explanation: The subsequence {3, 4, 5, 6, 7, 8} is the longest increasing subsequenc
5 min read
Longest subsequence with different adjacent characters Given string str. The task is to find the longest subsequence of str such that all the characters adjacent to each other in the subsequence are different. Examples:Â Â Input: str = "ababa"Â Output: 5Â Explanation:Â "ababa" is the subsequence satisfying the condition Input: str = "xxxxy"Â Output: 2Â Explan
14 min read