Count number of rotated strings which have more number of vowels in the first half than second half
Given string str of even size N consisting of lowercase English alphabets. The task is to find the number of rotated strings of str which have more vowels in the first half than the second half.
Examples:
Input: str = “abcd”
Output: 2
Explanation:
All rotated string are “abcd”, “dabc”, “cdab”, “bcda”.
The first two rotated strings have more vowels in
the first half than the second half.
Input: str = “abecidft”
Output: 4
Explanation:
There are 4 possible strings with rotation where there are more vowels in first half than in the second half.
Approach: An efficient approach is to make string s = str + str then the size of the s will be 2 * N. Now, make a prefix array to store the number of vowels present from the 0th index to the ith index. Then run a loop from N – 1 to 2 * N – 2 to get all the rotated strings of str. Find the count of required rotated strings.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int cntRotations(string s, int n)
{
string str = s + s;
int pre[2 * n] = { 0 };
for ( int i = 0; i < 2 * n; i++) {
if (i != 0)
pre[i] += pre[i - 1];
if (str[i] == 'a' || str[i] == 'e'
|| str[i] == 'i' || str[i] == 'o'
|| str[i] == 'u' ) {
pre[i]++;
}
}
int ans = 0;
for ( int i = n - 1; i < 2 * n - 1; i++) {
int r = i, l = i - n;
int x1 = pre[r];
if (l >= 0)
x1 -= pre[l];
r = i - n / 2;
int left = pre[r];
if (l >= 0)
left -= pre[l];
int right = x1 - left;
if (left > right) {
ans++;
}
}
return ans;
}
int main()
{
string s = "abecidft" ;
int n = s.length();
cout << cntRotations(s, n);
return 0;
}
|
Java
class GFG
{
static int cntRotations(String s, int n)
{
String str = s + s;
int pre[]= new int [ 2 * n] ;
for ( int i = 0 ; i < 2 * n; i++)
{
if (i != 0 )
pre[i] += pre[i - 1 ];
if (str.charAt(i) == 'a' || str.charAt(i) == 'e' ||
str.charAt(i) == 'i' || str.charAt(i) == 'o' ||
str.charAt(i) == 'u' )
{
pre[i]++;
}
}
int ans = 0 ;
for ( int i = n - 1 ; i < 2 * n - 1 ; i++)
{
int r = i, l = i - n;
int x1 = pre[r];
if (l >= 0 )
x1 -= pre[l];
r = i - n / 2 ;
int left = pre[r];
if (l >= 0 )
left -= pre[l];
int right = x1 - left;
if (left > right)
{
ans++;
}
}
return ans;
}
public static void main(String args[])
{
String s = "abecidft" ;
int n = s.length();
System.out.println( cntRotations(s, n));
}
}
|
Python3
def cntRotations(s, n):
str = s + s;
pre = [ 0 ] * ( 2 * n);
for i in range ( 2 * n):
if (i ! = 0 ):
pre[i] + = pre[i - 1 ];
if ( str [i] = = 'a' or str [i] = = 'e' or
str [i] = = 'i' or str [i] = = 'o' or
str [i] = = 'u' ):
pre[i] + = 1 ;
ans = 0 ;
for i in range (n - 1 , 2 * n - 1 , 1 ):
r = i; l = i - n;
x1 = pre[r];
if (l > = 0 ):
x1 - = pre[l];
r = ( int )(i - n / 2 );
left = pre[r];
if (l > = 0 ):
left - = pre[l];
right = x1 - left;
if (left > right):
ans + = 1 ;
return ans;
s = "abecidft" ;
n = len (s);
print (cntRotations(s, n));
|
C#
using System;
class GFG
{
static int cntRotations( string s, int n)
{
string str = s + s;
int []pre = new int [2 * n];
for ( int i = 0; i < 2 * n; i++)
{
if (i != 0)
pre[i] += pre[i - 1];
if (str[i] == 'a' || str[i] == 'e' ||
str[i] == 'i' || str[i] == 'o' ||
str[i] == 'u' )
{
pre[i]++;
}
}
int ans = 0;
for ( int i = n - 1; i < 2 * n - 1; i++)
{
int r = i, l = i - n;
int x1 = pre[r];
if (l >= 0)
x1 -= pre[l];
r = i - n / 2;
int left = pre[r];
if (l >= 0)
left -= pre[l];
int right = x1 - left;
if (left > right)
{
ans++;
}
}
return ans;
}
public static void Main()
{
String s = "abecidft" ;
int n = s.Length;
Console.WriteLine( cntRotations(s, n));
}
}
|
Javascript
<script>
function cntRotations(s, n)
{
let str = s + s;
let pre = new Array(2 * n);
pre.fill(0);
for (let i = 0; i < 2 * n; i++)
{
if (i != 0)
pre[i] += pre[i - 1];
if (str[i] == 'a' || str[i] == 'e' ||
str[i] == 'i' || str[i] == 'o' ||
str[i] == 'u' )
{
pre[i]++;
}
}
let ans = 0;
for (let i = n - 1; i < 2 * n - 1; i++)
{
let r = i, l = i - n;
let x1 = pre[r];
if (l >= 0)
x1 -= pre[l];
r = i - parseInt(n / 2, 10);
let left = pre[r];
if (l >= 0)
left -= pre[l];
let right = x1 - left;
if (left > right)
{
ans++;
}
}
return ans;
}
let s = "abecidft" ;
let n = s.length;
document.write(cntRotations(s, n));
</script>
|
Time Complexity: O(2 * N)
Auxiliary Space: O(2 * N)
Efficient Approach: To reduce the Space complexity to constant for the above approach, store the number of vowels in both halves in two variables and iterate through all rotation by changing the index.
- In each rotation, the first element of the first-half gets removed and inserted into the second-half and if this element is a vowel then decrease the number of vowels in the first-half by 1 and increase the number of vowels in the second-half by 1.
- The first element of the second-half gets removed and inserted into the first-half and if this element is a vowel then increase the number of vowels in the first-half by 1 and decrease the number of vowels in the second-half by 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int cntRotations( char s[], int n)
{
int lh = 0, rh = 0, i, ans = 0;
for (i = 0; i < n / 2; ++i)
if (s[i] == 'a' || s[i] == 'e' ||
s[i] == 'i' || s[i] == 'o' ||
s[i] == 'u' )
{
lh++;
}
for (i = n / 2; i < n; ++i)
if (s[i] == 'a' || s[i] == 'e' ||
s[i] == 'i' || s[i] == 'o' ||
s[i] == 'u' )
{
rh++;
}
if (lh > rh)
ans++;
for (i = 1; i < n; ++i)
{
if (s[i - 1] == 'a' || s[i - 1] == 'e' ||
s[i - 1] == 'i' || s[i - 1] == 'o' ||
s[i - 1] == 'u' )
{
rh++;
lh--;
}
if (s[(i - 1 + n / 2) % n] == 'a' ||
s[(i - 1 + n / 2) % n] == 'e' ||
s[(i - 1 + n / 2) % n] == 'i' ||
s[(i - 1 + n / 2) % n] == 'o' ||
s[(i - 1 + n / 2) % n] == 'u' )
{
rh--;
lh++;
}
if (lh > rh)
ans++;
}
return ans;
}
int main()
{
char s[] = "abecidft" ;
int n = strlen (s);
cout << " " << cntRotations(s, n);
return 0;
}
|
C
#include <stdio.h>
#include <string.h>
int cntRotations( char s[], int n)
{
int lh = 0, rh = 0, i, ans = 0;
for (i = 0; i < n / 2; ++i)
if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i'
|| s[i] == 'o' || s[i] == 'u' )
{
lh++;
}
for (i = n / 2; i < n; ++i)
if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i'
|| s[i] == 'o' || s[i] == 'u' ) {
rh++;
}
if (lh > rh)
ans++;
for (i = 1; i < n; ++i) {
if (s[i - 1] == 'a' || s[i - 1] == 'e'
|| s[i - 1] == 'i' || s[i - 1] == 'o'
|| s[i - 1] == 'u' ) {
rh++;
lh--;
}
if (s[(i - 1 + n / 2) % n] == 'a'
|| s[(i - 1 + n / 2) % n] == 'e'
|| s[(i - 1 + n / 2) % n] == 'i'
|| s[(i - 1 + n / 2) % n] == 'o'
|| s[(i - 1 + n / 2) % n] == 'u' ) {
rh--;
lh++;
}
if (lh > rh)
ans++;
}
return ans;
}
int main()
{
char s[] = "abecidft" ;
int n = strlen (s);
printf ( "%d" , cntRotations(s, n));
return 0;
}
|
Java
class GFG{
public static int cntRotations( char s[],
int n)
{
int lh = 0 , rh = 0 , i, ans = 0 ;
for (i = 0 ; i < n / 2 ; ++i)
if (s[i] == 'a' || s[i] == 'e' ||
s[i] == 'i' || s[i] == 'o' ||
s[i] == 'u' )
{
lh++;
}
for (i = n / 2 ; i < n; ++i)
if (s[i] == 'a' || s[i] == 'e' ||
s[i] == 'i' || s[i] == 'o' ||
s[i] == 'u' )
{
rh++;
}
if (lh > rh)
ans++;
for (i = 1 ; i < n; ++i)
{
if (s[i - 1 ] == 'a' || s[i - 1 ] == 'e' ||
s[i - 1 ] == 'i' || s[i - 1 ] == 'o' ||
s[i - 1 ] == 'u' )
{
rh++;
lh--;
}
if (s[(i - 1 + n / 2 ) % n] == 'a' ||
s[(i - 1 + n / 2 ) % n] == 'e' ||
s[(i - 1 + n / 2 ) % n] == 'i' ||
s[(i - 1 + n / 2 ) % n] == 'o' ||
s[(i - 1 + n / 2 ) % n] == 'u' )
{
rh--;
lh++;
}
if (lh > rh)
ans++;
}
return ans;
}
public static void main(String[] args)
{
char s[] = { 'a' , 'b' , 'e' , 'c' ,
'i' , 'd' , 'f' , 't' };
int n = s.length;
System.out.println(
cntRotations(s, n));
}
}
|
Python3
def cntRotations(s, n):
lh, rh, ans = 0 , 0 , 0
for i in range (n / / 2 ):
if (s[i] = = 'a' or s[i] = = 'e' or
s[i] = = 'i' or s[i] = = 'o' or
s[i] = = 'u' ):
lh + = 1
for i in range (n / / 2 , n):
if (s[i] = = 'a' or s[i] = = 'e' or
s[i] = = 'i' or s[i] = = 'o' or
s[i] = = 'u' ):
rh + = 1
if (lh > rh):
ans + = 1
for i in range ( 1 , n):
if (s[i - 1 ] = = 'a' or s[i - 1 ] = = 'e' or
s[i - 1 ] = = 'i' or s[i - 1 ] = = 'o' or
s[i - 1 ] = = 'u' ):
rh + = 1
lh - = 1
if (s[(i - 1 + n / / 2 ) % n] = = 'a' or
s[(i - 1 + n / / 2 ) % n] = = 'e' or
s[(i - 1 + n / / 2 ) % n] = = 'i' or
s[(i - 1 + n / / 2 ) % n] = = 'o' or
s[(i - 1 + n / / 2 ) % n] = = 'u' ):
rh - = 1
lh + = 1
if (lh > rh):
ans + = 1
return ans
if __name__ = = "__main__" :
s = "abecidft"
n = len (s)
print (cntRotations(s, n))
|
C#
using System;
class GFG
{
static int cntRotations( char [] s, int n)
{
int lh = 0, rh = 0, i, ans = 0;
for (i = 0; i < n / 2; ++i)
if (s[i] == 'a' || s[i] == 'e' ||
s[i] == 'i' || s[i] == 'o' ||
s[i] == 'u' )
{
lh++;
}
for (i = n / 2; i < n; ++i)
if (s[i] == 'a' || s[i] == 'e' ||
s[i] == 'i' || s[i] == 'o' ||
s[i] == 'u' )
{
rh++;
}
if (lh > rh)
ans++;
for (i = 1; i < n; ++i)
{
if (s[i - 1] == 'a' || s[i - 1] == 'e' ||
s[i - 1] == 'i' || s[i - 1] == 'o' ||
s[i - 1] == 'u' )
{
rh++;
lh--;
}
if (s[(i - 1 + n / 2) % n] == 'a' ||
s[(i - 1 + n / 2) % n] == 'e' ||
s[(i - 1 + n / 2) % n] == 'i' ||
s[(i - 1 + n / 2) % n] == 'o' ||
s[(i - 1 + n / 2) % n] == 'u' )
{
rh--;
lh++;
}
if (lh > rh)
ans++;
}
return ans;
}
static void Main()
{
char [] s = { 'a' , 'b' , 'e' , 'c' ,
'i' , 'd' , 'f' , 't' };
int n = s.Length;
Console.WriteLine(cntRotations(s, n));
}
}
|
Javascript
<script>
function cntRotations(s, n)
{
let lh = 0, rh = 0, i, ans = 0;
for (i = 0; i < parseInt(n / 2, 10); ++i)
if (s[i] == 'a' || s[i] == 'e' ||
s[i] == 'i' || s[i] == 'o' ||
s[i] == 'u' )
{
lh++;
}
for (i = parseInt(n / 2, 10); i < n; ++i)
if (s[i] == 'a' || s[i] == 'e' ||
s[i] == 'i' || s[i] == 'o' ||
s[i] == 'u' )
{
rh++;
}
if (lh > rh)
ans++;
for (i = 1; i < n; ++i)
{
if (s[i - 1] == 'a' || s[i - 1] == 'e' ||
s[i - 1] == 'i' || s[i - 1] == 'o' ||
s[i - 1] == 'u' )
{
rh++;
lh--;
}
if (s[(i - 1 + n / 2) % n] == 'a' ||
s[(i - 1 + n / 2) % n] == 'e' ||
s[(i - 1 + n / 2) % n] == 'i' ||
s[(i - 1 + n / 2) % n] == 'o' ||
s[(i - 1 + n / 2) % n] == 'u' )
{
rh--;
lh++;
}
if (lh > rh)
ans++;
}
return ans;
}
let s = [ 'a' , 'b' , 'e' , 'c' , 'i' , 'd' , 'f' , 't' ];
let n = s.length;
document.write(cntRotations(s, n));
</script>
|
Time Complexity: O(n)
Space Complexity: O(1)
Last Updated :
21 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...