给定一个由小写字母组成的大小为N的字符串S ,任务是打印仅由按非递增顺序排序的元音组成的最长子字符串的长度。
例子:
Input: S = “ueiaoaeiouuoiea”
Output: 6
Explanation: The only substring which consists only of vowels in non-increasing order, is the substring {S[9], S[15]}, which is “uuoiea”.
Input: S = “uuuioea”
Output: 0
方法:可以使用HashSet解决给定的问题。请按照以下步骤解决问题:
- 以降序将所有元音存储在一个数组中,比如ch[] 。
- 初始化三个变量res、j、 count为0 ,分别存储所需子串的最长长度,迭代所有元音,分别存储当前满足条件的子串的长度。
- 此外,初始化一个 HashSet 说mp来存储满足条件的当前子字符串的所有不同字符。
- 使用变量i迭代字符串S的字符并执行以下操作:
- 如果S[i]等于ch[j]则将S[i]添加到mp然后将j和count增加1 。如果mp的大小等于5 ,则将res更新为res和count的最大值。
- 否则,如果j + 1小于5并且S[i]等于ch[j + 1] ,则将j和count增加1并将S[i]添加到mp 。如果mp的大小等于5 ,则将res更新为res和count的最大值。
- 否则,如果S[i]等于 ‘ u ‘,则将0分配给j ,将1分配给count并将S[i]添加到mp 。否则,将0分配给j和count 。
- 完成上述步骤后,打印res的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find length of the
// longest substring consisting only
// of vowels in non-increasing order
int count(string S, int N)
{
// Stores all vowels in decreasing order
char ch[] = { 'u', 'o', 'i', 'e', 'a' };
// Stores current index of array ch[]
int j = 0;
// Stores the result
int res = 0;
// Stores the count
// of current substring
int count = 0;
// Declare a HashSet to store the vowels
unordered_set mp;
// Traverse the string, S
for(int i = 0; i < N; ++i)
{
// If S[i] is equal to ch[j]
if (S[i] == ch[j])
{
// Increment count by 1
++count;
// Add S[i] in the mp
mp.insert(S[i]);
// If length of mp is 5, update res
if (mp.size() == 5)
{
res = max(res, count);
}
}
// Else if j+1 is less than 5 and
// S[i] is equal to ch[j+1]
else if (j + 1 < 5 && S[i] == ch[j + 1])
{
// Add the S[i] in the mp
mp.insert(S[i]);
// Increment count by 1
++count;
// Increment j by 1
j++;
// If length of mp is 5, update res
if (mp.size() == 5)
{
res = max(res, count);
}
}
else
{
// Clear the mp
mp.clear();
// If S[i] is 'u'
if (S[i] == 'u')
{
// Add S[i] in the mp
mp.insert('u');
// Update j and assign 1 to count
j = 0;
count = 1;
}
// Else assign 0 to j and count
else
{
j = 0;
count = 0;
}
}
}
// Return the result
return res;
}
// Driver Code
int main()
{
string S = "ueiaoaeiouuoiea";
int N = S.size();
// Function Call
cout << count(S, N);
return 0;
}
// This code is contributed by Kingash
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG {
// Function to find length of the
// longest substring consisting only
// of vowels in non-increasing order
static int count(String S, int N)
{
// Stores all vowels in decreasing order
char ch[] = { 'u', 'o', 'i', 'e', 'a' };
// Stores current index of array ch[]
int j = 0;
// Stores the result
int res = 0;
// Stores the count
// of current substring
int count = 0;
// Declare a HashSet to store the vowels
HashSet mp = new HashSet<>();
// Traverse the string, S
for (int i = 0; i < N; ++i) {
// If S[i] is equal to ch[j]
if (S.charAt(i) == ch[j]) {
// Increment count by 1
++count;
// Add S[i] in the mp
mp.add(S.charAt(i));
// If length of mp is 5, update res
if (mp.size() == 5) {
res = Math.max(res, count);
}
}
// Else if j+1 is less than 5 and
// S[i] is equal to ch[j+1]
else if (j + 1 < 5
&& S.charAt(i) == ch[j + 1]) {
// Add the S[i] in the mp
mp.add(S.charAt(i));
// Increment count by 1
++count;
// Increment j by 1
j++;
// If length of mp is 5, update res
if (mp.size() == 5) {
res = Math.max(res, count);
}
}
else {
// Clear the mp
mp.clear();
// If S[i] is 'u'
if (S.charAt(i) == 'u') {
// Add S[i] in the mp
mp.add('u');
// Update j and assign 1 to count
j = 0;
count = 1;
}
// Else assign 0 to j and count
else {
j = 0;
count = 0;
}
}
}
// Return the result
return res;
}
// Driver Code
public static void main(String[] args)
{
// Given Input
String S = "ueiaoaeiouuoiea";
int N = S.length();
// Function Call
System.out.println(count(S, N));
}
}
输出:
6
时间复杂度: O(N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live