给定一个包含小写英文字母的字符串S ,任务是从给定的字符串中找到具有相等数量的元音和辅音的最长子字符串的长度。
例子:
Input: S = “geeksforgeeks”
Output: 10
Explanation:
Substring “eeksforgee” consists of 5 vowels and 5 consonants. Remaining characters are only consonants. Therefore, any longer substring won’t have equal number of vowels and consonants.
Input: S = “qwertyuiop”
Output: 8
Explanation:
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
输出:
10
时间复杂度: O(NlogN)
辅助空间: O(N)