Longest substring of vowels with no two adjacent alphabets same
Last Updated :
02 Jan, 2023
Given string str consisting of lowercase alphabets, the task is to find the length of the longest substring such that all of its characters are vowels and no two adjacent alphabets are the same.
Examples:
Input: str = “aeoibsddaeiouudb”
Output: 5
Explanation:
The longest substring of vowels in which no two adjacent letters are same is “aeiou”
Length of substring = 5
Input: str = “geeksforgeeks”
Output: 1
Explanation:
The longest substring of vowels in which no two adjacent letters are same are “e” or “o”.
Length of substring = 1
Naive Approach:
Generate all possible substrings from the given string. For each substring, check if all of its characters are vowels and no two adjacent characters are the same then compare the length of all such substrings and print the maximum length.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isVowel( char c)
{
return (c == 'a' || c == 'e'
|| c == 'i' || c == 'o'
|| c == 'u' );
}
bool isValid(string& s)
{
int n = s.size();
if (n == 1)
return (isVowel(s[0]));
if (isVowel(s[0]) == false )
return false ;
for ( int i = 1; i < n; i++) {
if (s[i] == s[i - 1]
|| !isVowel(s[i]))
return false ;
}
return true ;
}
int findMaxLen(string& s)
{
int maxLen = 0;
int n = s.length();
for ( int i = 0; i < n; i++) {
string temp = "" ;
for ( int j = i; j < n; j++) {
temp = temp + s[j];
if (isValid(temp))
maxLen = max(
maxLen, (j - i + 1));
}
}
return maxLen;
}
int main()
{
string Str = "aeoibsddaeiouudb" ;
cout << findMaxLen(Str) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG{
static boolean isVowel( char c)
{
return (c == 'a' || c == 'e' ||
c == 'i' || c == 'o' ||
c == 'u' );
}
static boolean isValid(String s)
{
int n = s.length();
if (n == 1 )
return (isVowel(s.charAt( 0 )));
if (isVowel(s.charAt( 0 )) == false )
return false ;
for ( int i = 1 ; i < n; i++)
{
if (s.charAt(i) == s.charAt(i - 1 ) ||
!isVowel(s.charAt(i)))
return false ;
}
return true ;
}
static int findMaxLen(String s)
{
int maxLen = 0 ;
int n = s.length();
for ( int i = 0 ; i < n; i++)
{
String temp = "" ;
for ( int j = i; j < n; j++)
{
temp = temp + s.charAt(j);
if (isValid(temp))
maxLen = Math.max(maxLen,
(j - i + 1 ));
}
}
return maxLen;
}
public static void main (String[] args)
{
String Str = "aeoibsddaeiouudb" ;
System.out.println(findMaxLen(Str));
}
}
|
Python3
def isVowel(c):
return (c = = 'a' or c = = 'e' or
c = = 'i' or c = = 'o' or
c = = 'u' )
def isValid(s):
n = len (s)
if (n = = 1 ):
return (isVowel(s[ 0 ]))
if (isVowel(s[ 0 ]) = = False ):
return False
for i in range ( 1 , n):
if (s[i] = = s[i - 1 ] or not
isVowel(s[i])):
return False
return True
def findMaxLen(s):
maxLen = 0
n = len (s)
for i in range (n):
temp = ""
for j in range (i, n):
temp = temp + s[j]
if (isValid(temp)):
maxLen = ( max (maxLen, (j - i + 1 )))
return maxLen
if __name__ = = "__main__" :
Str = "aeoibsddaeiouudb"
print (findMaxLen( Str ))
|
C#
using System;
class GFG{
static bool isVowel( char c)
{
return (c == 'a' || c == 'e' ||
c == 'i' || c == 'o' ||
c == 'u' );
}
static bool isValid( string s)
{
int n = s.Length;
if (n == 1)
return (isVowel(s[0]));
if (isVowel(s[0]) == false )
return false ;
for ( int i = 1; i < n; i++)
{
if (s[i] == s[i - 1] ||
!isVowel(s[i]))
return false ;
}
return true ;
}
static int findMaxLen( string s)
{
int maxLen = 0;
int n = s.Length;
for ( int i = 0; i < n; i++)
{
string temp = "" ;
for ( int j = i; j < n; j++)
{
temp = temp + s[j];
if (isValid(temp))
maxLen = Math.Max(maxLen,
(j - i + 1));
}
}
return maxLen;
}
public static void Main()
{
string Str = "aeoibsddaeiouudb" ;
Console.WriteLine(findMaxLen(Str));
}
}
|
Javascript
<script>
function isVowel(c)
{
return (c == 'a' || c == 'e' ||
c == 'i' || c == 'o' ||
c == 'u' );
}
function isValid(s)
{
let n = s.length;
if (n == 1)
return (isVowel(s[0]));
if (isVowel(s[0]) == false )
return false ;
for (let i = 1; i < n; i++)
{
if (s[i] == s[i - 1] ||
!isVowel(s[i]))
return false ;
}
return true ;
}
function findMaxLen(s)
{
let maxLen = 0;
let n = s.length;
for (let i = 0; i < n; i++)
{
let temp = "" ;
for (let j = i; j < n; j++)
{
temp = temp + s[j];
if (isValid(temp))
maxLen = Math.max(maxLen,
(j - i + 1));
}
}
return maxLen;
}
let Str = "aeoibsddaeiouudb" ;
document.write(findMaxLen(Str));
</script>
|
Time Complexity: O(N3)
Auxiliary Space: O(N), where N is the length of the given string.
Efficient Approach:
A linear-time solution is possible for this problem. A substring will be considered as valid if all of its characters are vowel and no two adjacent ones are the same. The idea is to traverse the string and keep track of the longest valid substring.
The following are the detailed steps:
- Create a variable cur to store the length of current valid substring and another variable maxVal to keep track of maximum cur found so far, initially, both are set to 0.
- If the character at index 0 is a vowel, then it is a valid substring of length 1, so maxVal = 1
- Traverse str starting from index 1. If the current character is not a vowel, no valid substring present, cur = 0
- If the current character is a vowel and
- Is different from the previous character, then includes it in the current valid substring and increment cur by 1. Update maxVal.
- Is the same as the previous character, then a new valid substring starts from this character with cur reset to 1.
- The final value of maxVal gives the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isvowel( char c)
{
return c == 'a' || c == 'e'
|| c == 'i' || c == 'o'
|| c == 'u' ;
}
int findMaxLen(string& s)
{
int maxLen = 0;
int cur = 0;
if (isvowel(s[0]))
cur = maxLen = 1;
for ( int i = 1; i < s.length(); i++) {
if (isvowel(s[i])) {
if (s[i] != s[i - 1])
cur += 1;
else
cur = 1;
}
else {
cur = 0;
}
maxLen = max(cur, maxLen);
}
return maxLen;
}
int main()
{
string Str = "aeoibsddaeiouudb" ;
cout << findMaxLen(Str) << endl;
return 0;
}
|
Java
public class GFG {
static boolean isVowel( char x)
{
return (x == 'a' || x == 'e'
|| x == 'i' || x == 'o'
|| x == 'u' );
}
static int findMaxLen(String s)
{
int maxLen = 0 ;
int cur;
if (isVowel(s.charAt( 0 )))
maxLen = 1 ;
cur = maxLen;
for ( int i = 1 ; i < s.length(); i++) {
if (isVowel(s.charAt(i))) {
if (s.charAt(i)
!= s.charAt(i - 1 ))
cur += 1 ;
else
cur = 1 ;
}
else {
cur = 0 ;
}
maxLen = Math.max(cur, maxLen);
}
return maxLen;
}
public static void main(String[] args)
{
String Str = "aeoibsddaeiouudb" ;
System.out.println(findMaxLen(Str));
}
}
|
Python3
def isVowel(x):
return (x = = 'a' or x = = 'e' or
x = = 'i' or x = = 'o' or
x = = 'u' );
def findMaxLen(s):
maxLen = 0
cur = 0
if (isVowel(s[ 0 ])):
maxLen = 1 ;
cur = maxLen
for i in range ( 1 , len (s)):
if (isVowel(s[i])):
if (s[i] ! = s[i - 1 ]):
cur + = 1
else :
cur = 1
else :
cur = 0 ;
maxLen = max (cur, maxLen);
return maxLen
Str = "aeoibsddaeiouudb"
print (findMaxLen( Str ))
|
C#
using System;
public class GFG {
public static bool isVowel( char x)
{
return (x == 'a' || x == 'e'
|| x == 'i' || x == 'o'
|| x == 'u' );
}
public static int findMaxLen( string s)
{
int maxLen = 0;
int cur;
if (isVowel(s[0]))
maxLen = 1;
cur = maxLen;
for ( int i = 1; i < s.Length; i++) {
if (isVowel(s[i])) {
if (s[i] != s[i - 1])
cur += 1;
else
cur = 1;
}
else {
cur = 0;
}
maxLen = Math.Max(cur, maxLen);
}
return maxLen;
}
public static void Main( string [] args)
{
string Str = "aeoibsddaeiouudb" ;
Console.WriteLine(findMaxLen(Str));
}
}
|
Javascript
<script>
function isVowel(x) {
return x === "a" || x === "e" || x === "i" || x === "o" || x === "u" ;
}
function findMaxLen(s)
{
var maxLen = 0;
var cur;
if (isVowel(s[0])) maxLen = 1;
cur = maxLen;
for ( var i = 1; i < s.length; i++) {
if (isVowel(s[i])) {
if (s[i] !== s[i - 1]) cur += 1;
else cur = 1;
} else {
cur = 0;
}
maxLen = Math.max(cur, maxLen);
}
return maxLen;
}
var Str = "aeoibsddaeiouudb" ;
document.write(findMaxLen(Str));
</script>
|
Time complexity: O(N)
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...