# Count number of rotated strings which have more number of vowels in the first half than second half

Given a 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
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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the count of rotated ` `// strings which have more number of vowels in ` `// the first half than the second half ` `int` `cntRotations(string s, ``int` `n) ` `{ ` `    ``// Create a new string ` `    ``string str = s + s; ` ` `  `    ``// Pre array to store count of all vowels ` `    ``int` `pre[2 * n] = { 0 }; ` ` `  `    ``// Compute the prefix array ` `    ``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]++; ` `        ``} ` `    ``} ` ` `  `    ``// To store the required answer ` `    ``int` `ans = 0; ` ` `  `    ``// Find all rotated strings ` `    ``for` `(``int` `i = n - 1; i < 2 * n - 1; i++) { ` ` `  `        ``// Right and left index of the string ` `        ``int` `r = i, l = i - n; ` ` `  `        ``// x1 stores the number of vowels ` `        ``// in the rotated string ` `        ``int` `x1 = pre[r]; ` `        ``if` `(l >= 0) ` `            ``x1 -= pre[l]; ` `        ``r = i - n / 2; ` ` `  `        ``// Left stores the number of vowels ` `        ``// in the first half of rotated string ` `        ``int` `left = pre[r]; ` `        ``if` `(l >= 0) ` `            ``left -= pre[l]; ` ` `  `        ``// Right stores the number of vowels ` `        ``// in the second half of rotated string ` `        ``int` `right = x1 - left; ` ` `  `        ``// If the count of vowels in the first half ` `        ``// is greater than the count in the second half ` `        ``if` `(left > right) { ` `            ``ans++; ` `        ``} ` `    ``} ` ` `  `    ``// Return the required answer ` `    ``return` `ans; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string s = ``"abecidft"``; ` `    ``int` `n = s.length(); ` ` `  `    ``cout << cntRotations(s, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `class` `GFG ` `{ ` ` `  `// Function to return the count of rotated ` `// Strings which have more number of vowels in ` `// the first half than the second half ` `static` `int` `cntRotations(String s, ``int` `n) ` `{ ` `    ``// Create a new String ` `    ``String str = s + s; ` ` `  `    ``// Pre array to store count of all vowels ` `    ``int` `pre[]=``new` `int``[``2` `* n] ; ` ` `  `    ``// Compute the prefix array ` `    ``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]++; ` `        ``} ` `    ``} ` ` `  `    ``// To store the required answer ` `    ``int` `ans = ``0``; ` ` `  `    ``// Find all rotated Strings ` `    ``for` `(``int` `i = n - ``1``; i < ``2` `* n - ``1``; i++)  ` `    ``{ ` ` `  `        ``// Right and left index of the String ` `        ``int` `r = i, l = i - n; ` ` `  `        ``// x1 stores the number of vowels ` `        ``// in the rotated String ` `        ``int` `x1 = pre[r]; ` `        ``if` `(l >= ``0``) ` `            ``x1 -= pre[l]; ` `        ``r = i - n / ``2``; ` ` `  `        ``// Left stores the number of vowels ` `        ``// in the first half of rotated String ` `        ``int` `left = pre[r]; ` `        ``if` `(l >= ``0``) ` `            ``left -= pre[l]; ` ` `  `        ``// Right stores the number of vowels ` `        ``// in the second half of rotated String ` `        ``int` `right = x1 - left; ` ` `  `        ``// If the count of vowels in the first half ` `        ``// is greater than the count in the second half ` `        ``if` `(left > right)  ` `        ``{ ` `            ``ans++; ` `        ``} ` `    ``} ` ` `  `    ``// Return the required answer ` `    ``return` `ans; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``String s = ``"abecidft"``; ` `    ``int` `n = s.length(); ` ` `  `    ``System.out.println( cntRotations(s, n)); ` `} ` `} ` ` `  `// This code is contributed by Arnab Kundu `

## Python3

 `# Python3 implementation of the approach ` ` `  `# Function to return the count of rotated ` `# strings which have more number of vowels in ` `# the first half than the second half ` `def` `cntRotations(s, n): ` ` `  `    ``# Create a new string ` `    ``str` `=` `s ``+` `s; ` ` `  `    ``# Pre array to store count of all vowels ` `    ``pre ``=` `[``0``] ``*` `(``2` `*` `n); ` ` `  `    ``# Compute the prefix array ` `    ``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``; ` `         `  `    ``# To store the required answer ` `    ``ans ``=` `0``; ` ` `  `    ``# Find all rotated strings ` `    ``for` `i ``in` `range``(n ``-` `1``, ``2` `*` `n ``-` `1``, ``1``): ` ` `  `        ``# Right and left index of the string ` `        ``r ``=` `i; l ``=` `i ``-` `n; ` ` `  `        ``# x1 stores the number of vowels ` `        ``# in the rotated string ` `        ``x1 ``=` `pre[r]; ` `        ``if` `(l >``=` `0``): ` `            ``x1 ``-``=` `pre[l]; ` `        ``r ``=` `(``int``)(i ``-` `n ``/` `2``); ` ` `  `        ``# Left stores the number of vowels ` `        ``# in the first half of rotated string ` `        ``left ``=` `pre[r]; ` `        ``if` `(l >``=` `0``): ` `            ``left ``-``=` `pre[l]; ` ` `  `        ``# Right stores the number of vowels ` `        ``# in the second half of rotated string ` `        ``right ``=` `x1 ``-` `left; ` ` `  `        ``# If the count of vowels in the first half ` `        ``# is greater than the count in the second half ` `        ``if` `(left > right):  ` `            ``ans ``+``=` `1``; ` `         `  `    ``# Return the required answer ` `    ``return` `ans; ` ` `  `# Driver code ` `s ``=` `"abecidft"``; ` `n ``=` `len``(s); ` ` `  `print``(cntRotations(s, n)); ` ` `  `# This code is contributed by Rajput-Ji `

## C#

 `// C# implementation of the approach  ` `using` `System; ` ` `  `class` `GFG  ` `{  ` `     `  `    ``// Function to return the count of rotated  ` `    ``// Strings which have more number of vowels in  ` `    ``// the first half than the second half  ` `    ``static` `int` `cntRotations(``string` `s, ``int` `n)  ` `    ``{  ` `        ``// Create a new String  ` `        ``string` `str = s + s;  ` `     `  `        ``// Pre array to store count of all vowels  ` `        ``int` `[]pre = ``new` `int``[2 * n];  ` `     `  `        ``// Compute the prefix array  ` `        ``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]++;  ` `            ``}  ` `        ``}  ` `     `  `        ``// To store the required answer  ` `        ``int` `ans = 0;  ` `     `  `        ``// Find all rotated Strings  ` `        ``for` `(``int` `i = n - 1; i < 2 * n - 1; i++)  ` `        ``{  ` `     `  `            ``// Right and left index of the String  ` `            ``int` `r = i, l = i - n;  ` `     `  `            ``// x1 stores the number of vowels  ` `            ``// in the rotated String  ` `            ``int` `x1 = pre[r];  ` `            ``if` `(l >= 0)  ` `                ``x1 -= pre[l];  ` `            ``r = i - n / 2;  ` `     `  `            ``// Left stores the number of vowels  ` `            ``// in the first half of rotated String  ` `            ``int` `left = pre[r];  ` `            ``if` `(l >= 0)  ` `                ``left -= pre[l];  ` `     `  `            ``// Right stores the number of vowels  ` `            ``// in the second half of rotated String  ` `            ``int` `right = x1 - left;  ` `     `  `            ``// If the count of vowels in the first half  ` `            ``// is greater than the count in the second half  ` `            ``if` `(left > right)  ` `            ``{  ` `                ``ans++;  ` `            ``}  ` `        ``}  ` `     `  `        ``// Return the required answer  ` `        ``return` `ans;  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `Main()  ` `    ``{  ` `        ``String s = ``"abecidft"``;  ` `        ``int` `n = s.Length;  ` `     `  `        ``Console.WriteLine( cntRotations(s, n));  ` `    ``}  ` `} ` ` `  `// This code is contributed by AnkitRai01  `

Output:

```4
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.