具有连续英文字母的最长子序列
给定字符串S ,任务是找到连续小写字母的最长子序列的长度。
例子:
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
朴素方法:最简单的方法是生成给定字符串的所有可能子序列,如果给定字符串的任何子字符串具有连续字符且具有最大长度,则打印该长度。
时间复杂度: O(2 N )
辅助空间: O(1)
高效方法:上述方法也可以通过从每个小写字母开始生成给定字符串的所有可能连续子序列并打印找到的所有子序列中的最大值来优化。请按照以下步骤解决问题:
- 初始化一个变量,比如ans,为0 ,它存储连续子序列的最大长度。
- 对于[a, z]范围内的每个字符ch ,执行以下操作:
- 将变量cnt初始化为0 ,该变量存储从ch开始的连续字符子序列的长度。
- 遍历给定的字符串S并且如果当前字符是ch则将cnt增加1并将当前字符ch 更新为下一个字符(ch + 1) 。
- 更新ans = max(ans, cnt)
- 经过以上步骤,打印出ans的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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
输出
7
时间复杂度: O(26*N)
辅助空间: O(1)