给定一个由小写英文字母组成的字符串S ,任务是从给定的字符串找出最长的子串的长度,它具有相同数量的元音和辅音。
例子:
Input: S = “geeksforgeeks”
Output: 10
Explanation:
The substring “eeksforgee” consists of 5 vowels and 5 consonants. Remaining characters are only consonants. Therefore, any longer substring won’t have an equal number of vowels and consonants.
Input: S = “qwertyuiop”
Output: 8
Explanation:
The substring “wertyuio” consists of 4 vowels and 4 consonants.
朴素的方法:最简单的解决方案是生成给定字符串的所有子串,并为每个子串检查元音和辅音的数量是否相等。最后,打印获得的元音和辅音数量相等的子串的最大长度。
时间复杂度: O(N 3 )
辅助空间: O(1)
高效方法:思路是考虑一个长度等于给定字符串的数组,分别存储元音和辅音对应的1和-1 ,使用HashMap打印出总和等于0的最长子数组的长度.
下面是上述方法的实现:
C++
// C++ Program to implement
// the above approach
#include
using namespace std;
// Function to return the length of
// the longest substring having equal
// number of vowel and consonant
int maxsubstringLength(string S, int N)
{
int arr[N];
// Generate the array
for (int i = 0; i < N; i++)
if (S[i] == 'a' || S[i] == 'e' || S[i] == 'i'
|| S[i] == 'o' || S[i] == 'u')
arr[i] = 1;
else
arr[i] = -1;
// Initialize variable
// to store result
int maxLen = 0;
// Stores the sum of subarray
int curr_sum = 0;
// Map to store indices of the sum
unordered_map hash;
// Loop through the array
for (int i = 0; i < N; i++) {
curr_sum += arr[i];
// If sum is 0
if (curr_sum == 0)
// Count of vowels and consonants
// are equal
maxLen = max(maxLen, i + 1);
// Update the maximum length
// of substring in HashMap
if (hash.find(curr_sum) != hash.end())
maxLen = max(maxLen, i - hash[curr_sum]);
// Store the index of the sum
else
hash[curr_sum] = i;
}
// Return the maximum
// length of required substring
return maxLen;
}
// Driver Code
int main()
{
string S = "geeksforgeeks";
int n = sizeof(S) / sizeof(S[0]);
cout << maxsubstringLength(S, n);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to return the length of
// the longest subString having equal
// number of vowel and consonant
static int maxsubStringLength(char[] S, int N)
{
int arr[] = new int[N];
// Generate the array
for(int i = 0; i < N; i++)
if (S[i] == 'a' || S[i] == 'e' ||
S[i] == 'i' || S[i] == 'o' ||
S[i] == 'u')
arr[i] = 1;
else
arr[i] = -1;
// Initialize variable
// to store result
int maxLen = 0;
// Stores the sum of subarray
int curr_sum = 0;
// Map to store indices of the sum
HashMap hash = new HashMap<>();
// Loop through the array
for(int i = 0; i < N; i++)
{
curr_sum += arr[i];
// If sum is 0
if (curr_sum == 0)
// Count of vowels and consonants
// are equal
maxLen = Math.max(maxLen, i + 1);
// Update the maximum length
// of subString in HashMap
if (hash.containsKey(curr_sum))
maxLen = Math.max(maxLen,
i - hash.get(curr_sum));
// Store the index of the sum
else
// hash[curr_sum] = i;
hash.put(curr_sum, i);
}
// Return the maximum
// length of required subString
return maxLen;
}
// Driver Code
public static void main(String[] args)
{
String S = "geeksforgeeks";
int n = S.length();
System.out.print(
maxsubStringLength(S.toCharArray(), n));
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 program to implement
# the above approach
# Function to return the length of
# the longest substring having equal
# number of vowel and consonant
def maxsubstringLength(S, N):
arr = [0] * N
# Generate the array
for i in range(N):
if(S[i] == 'a' or S[i] == 'e' or
S[i] == 'i' or S[i] == 'o' or
S[i] == 'u'):
arr[i] = 1
else:
arr[i] = -1
# Initialize variable
# to store result
maxLen = 0
# Stores the sum of subarray
curr_sum = 0
# Map to store indices of the sum
hash = {}
# Loop through the array
for i in range(N):
curr_sum += arr[i]
# If sum is 0
if(curr_sum == 0):
# Count of vowels and consonants
# are equal
maxLen = max(maxLen, i + 1)
# Update the maximum length
# of substring in HashMap
if(curr_sum in hash.keys()):
maxLen = max(maxLen, i - hash[curr_sum])
# Store the index of the sum
else:
hash[curr_sum] = i
# Return the maximum
# length of required substring
return maxLen
# Driver Code
S = "geeksforgeeks"
n = len(S)
# Function call
print(maxsubstringLength(S, n))
# This code is contributed by Shivam Singh
C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to return the length of
// the longest subString having equal
// number of vowel and consonant
static int maxsubStringLength(char[] S, int N)
{
int []arr = new int[N];
// Generate the array
for(int i = 0; i < N; i++)
if (S[i] == 'a' || S[i] == 'e' ||
S[i] == 'i' || S[i] == 'o' ||
S[i] == 'u')
arr[i] = 1;
else
arr[i] = -1;
// Initialize variable
// to store result
int maxLen = 0;
// Stores the sum of subarray
int curr_sum = 0;
// Map to store indices of the sum
Dictionary hash = new Dictionary();
// Loop through the array
for(int i = 0; i < N; i++)
{
curr_sum += arr[i];
// If sum is 0
if (curr_sum == 0)
// Count of vowels and consonants
// are equal
maxLen = Math.Max(maxLen, i + 1);
// Update the maximum length
// of subString in Dictionary
if (hash.ContainsKey(curr_sum))
maxLen = Math.Max(maxLen,
i - hash[curr_sum]);
// Store the index of the sum
else
// hash[curr_sum] = i;
hash.Add(curr_sum, i);
}
// Return the maximum
// length of required subString
return maxLen;
}
// Driver Code
public static void Main(String[] args)
{
String S = "geeksforgeeks";
int n = S.Length;
Console.Write(maxsubStringLength(
S.ToCharArray(), n));
}
}
// This code is contributed by Princi Singh
Javascript
10
时间复杂度: O(NlogN)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live