📜  计算上半个元音比下半个元音更多的旋转字符串数

📅  最后修改于: 2021-04-24 15:02:02             🧑  作者: Mango

给定大小为N的字符串str ,由小写英文字母组成。任务是找到旋转的str字符串的数量,这些字符串的上半部分比下半部分具有更多的元音。

例子:

方法:一种有效的方法是使字符串s = str + str,s的大小将为2 * N。现在,创建一个前缀数组来存储从第0个索引到第i个索引存在的元音数量。然后运行从N – 12 * N – 2的循环,以获取所有旋转的str字符串。查找所需的旋转字符串。

下面是上述方法的实现:

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


Javascript


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(char s[], int n)
{
    int lh = 0, rh = 0, i, ans = 0;
 
    // Compute the number of
    // vowels in first-half
    for(i = 0; i < n / 2; ++i)
        if (s[i] == 'a' || s[i] == 'e' ||
            s[i] == 'i' || s[i] == 'o' ||
            s[i] == 'u')
        {
            lh++;
        }
 
    // Compute the number of
    // vowels in second-half
    for(i = n / 2; i < n; ++i)
        if (s[i] == 'a' || s[i] == 'e' ||
            s[i] == 'i' || s[i] == 'o' ||
            s[i] == 'u')
        {
            rh++;
        }
 
    // Check if first-half
    // has more vowels
    if (lh > rh)
        ans++;
 
    // Check for all possible rotations
    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 the answer
    return ans;
}
 
// Driver code
int main()
{
    char s[] = "abecidft";
 
    int n = strlen(s);
 
    // Function call
    cout << " " << cntRotations(s, n);
 
    return 0;
}
 
// This code is contributed by shivanisinghss2110


C
// C implementation of the approach
#include 
#include 
 
// Function to return the count of
// rotated strings which have more
// number of vowels in the first
// half than the second half
int cntRotations(char s[], int n)
{
    int lh = 0, rh = 0, i, ans = 0;
 
    // Compute the number of
    // vowels in first-half
    for (i = 0; i < n / 2; ++i)
        if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i'
            || s[i] == 'o' || s[i] == 'u')
        {
            lh++;
        }
 
    // Compute the number of
    // vowels in second-half
    for (i = n / 2; i < n; ++i)
        if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i'
            || s[i] == 'o' || s[i] == 'u') {
            rh++;
        }
 
    // Check if first-half
    // has more vowels
    if (lh > rh)
        ans++;
 
    // Check for all possible rotations
    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 the answer
    return ans;
}
 
// Driver code
int main()
{
    char s[] = "abecidft";
 
    int n = strlen(s);
 
    // Function call
    printf("%d", 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
public static int cntRotations(char s[],
                               int n)
{
  int lh = 0, rh = 0, i, ans = 0;
 
  // Compute the number of
  // vowels in first-half
  for (i = 0; i < n / 2; ++i)
    if (s[i] == 'a' || s[i] == 'e' ||
        s[i] == 'i' || s[i] == 'o' ||
        s[i] == 'u')
    {
      lh++;
    }
 
  // Compute the number of
  // vowels in second-half
  for (i = n / 2; i < n; ++i)
    if (s[i] == 'a' || s[i] == 'e' ||
        s[i] == 'i' || s[i] == 'o' ||
        s[i] == 'u')
    {
      rh++;
    }
 
  // Check if first-half
  // has more vowels
  if (lh > rh)
    ans++;
 
  // Check for all possible
  // rotations
  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 the answer
  return ans;
}
 
// Driver code
public static void main(String[] args)
{
  char s[] = {'a','b','e','c',
              'i','d','f','t'};
  int n = s.length;
   
  // Function call
  System.out.println(
         cntRotations(s, n));
}
}
 
// This code is contributed by divyeshrabadiya07


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):
 
    lh, rh, ans = 0, 0, 0
 
    # Compute the number of
    # vowels in first-half
    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
 
    # Compute the number of
    # vowels in second-half
    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
 
    # Check if first-half
    # has more vowels
    if (lh > rh):
        ans += 1
 
    # Check for all possible rotations
    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 the answer
    return ans
 
# Driver code
if __name__ == "__main__":
   
    s = "abecidft"
    n = len(s)
 
    # Function call
    print(cntRotations(s, n))
 
# This code is contributed by Chitranayal


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(char[] s, int n)
    {
      int lh = 0, rh = 0, i, ans = 0;
      
      // Compute the number of
      // vowels in first-half
      for (i = 0; i < n / 2; ++i)
        if (s[i] == 'a' || s[i] == 'e' ||
            s[i] == 'i' || s[i] == 'o' ||
            s[i] == 'u')
        {
          lh++;
        }
      
      // Compute the number of
      // vowels in second-half
      for (i = n / 2; i < n; ++i)
        if (s[i] == 'a' || s[i] == 'e' ||
            s[i] == 'i' || s[i] == 'o' ||
            s[i] == 'u')
        {
          rh++;
        }
      
      // Check if first-half
      // has more vowels
      if (lh > rh)
        ans++;
      
      // Check for all possible
      // rotations
      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 the answer
      return ans;
    }
   
  // Driver code    
  static void Main()
  {
      char[] s = {'a','b','e','c',
              'i','d','f','t'};
      int n = s.Length;
        
      // Function call
      Console.WriteLine(cntRotations(s, n));
  }
}
 
// This code is contributed by divyesh072019


Javascript


输出
4

时间复杂度: O(2 * N)
辅助空间: O(2 * N)

高效方法:对于上述方法,为了将空间复杂度降低到恒定,请将两个半部中的元音数量存储在两个变量中,并通过更改索引来迭代所有旋转。

  • 在每个旋转中,上半部分的第一个元素将被删除并插入下半部分,如果此元素是元音,则将上半部分的元音数量减少1而将下半部分的元音数量增加-减半。
  • 下半部分的第一个元素被删除并插入上半部分,如果此元素是元音,则将上半部分的元音数量增加1,并将下半部分的元音数量减少1 。

下面是上述方法的实现:

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(char s[], int n)
{
    int lh = 0, rh = 0, i, ans = 0;
 
    // Compute the number of
    // vowels in first-half
    for(i = 0; i < n / 2; ++i)
        if (s[i] == 'a' || s[i] == 'e' ||
            s[i] == 'i' || s[i] == 'o' ||
            s[i] == 'u')
        {
            lh++;
        }
 
    // Compute the number of
    // vowels in second-half
    for(i = n / 2; i < n; ++i)
        if (s[i] == 'a' || s[i] == 'e' ||
            s[i] == 'i' || s[i] == 'o' ||
            s[i] == 'u')
        {
            rh++;
        }
 
    // Check if first-half
    // has more vowels
    if (lh > rh)
        ans++;
 
    // Check for all possible rotations
    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 the answer
    return ans;
}
 
// Driver code
int main()
{
    char s[] = "abecidft";
 
    int n = strlen(s);
 
    // Function call
    cout << " " << cntRotations(s, n);
 
    return 0;
}
 
// This code is contributed by shivanisinghss2110

C

// C implementation of the approach
#include 
#include 
 
// Function to return the count of
// rotated strings which have more
// number of vowels in the first
// half than the second half
int cntRotations(char s[], int n)
{
    int lh = 0, rh = 0, i, ans = 0;
 
    // Compute the number of
    // vowels in first-half
    for (i = 0; i < n / 2; ++i)
        if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i'
            || s[i] == 'o' || s[i] == 'u')
        {
            lh++;
        }
 
    // Compute the number of
    // vowels in second-half
    for (i = n / 2; i < n; ++i)
        if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i'
            || s[i] == 'o' || s[i] == 'u') {
            rh++;
        }
 
    // Check if first-half
    // has more vowels
    if (lh > rh)
        ans++;
 
    // Check for all possible rotations
    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 the answer
    return ans;
}
 
// Driver code
int main()
{
    char s[] = "abecidft";
 
    int n = strlen(s);
 
    // Function call
    printf("%d", 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
public static int cntRotations(char s[],
                               int n)
{
  int lh = 0, rh = 0, i, ans = 0;
 
  // Compute the number of
  // vowels in first-half
  for (i = 0; i < n / 2; ++i)
    if (s[i] == 'a' || s[i] == 'e' ||
        s[i] == 'i' || s[i] == 'o' ||
        s[i] == 'u')
    {
      lh++;
    }
 
  // Compute the number of
  // vowels in second-half
  for (i = n / 2; i < n; ++i)
    if (s[i] == 'a' || s[i] == 'e' ||
        s[i] == 'i' || s[i] == 'o' ||
        s[i] == 'u')
    {
      rh++;
    }
 
  // Check if first-half
  // has more vowels
  if (lh > rh)
    ans++;
 
  // Check for all possible
  // rotations
  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 the answer
  return ans;
}
 
// Driver code
public static void main(String[] args)
{
  char s[] = {'a','b','e','c',
              'i','d','f','t'};
  int n = s.length;
   
  // Function call
  System.out.println(
         cntRotations(s, n));
}
}
 
// This code is contributed by divyeshrabadiya07

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):
 
    lh, rh, ans = 0, 0, 0
 
    # Compute the number of
    # vowels in first-half
    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
 
    # Compute the number of
    # vowels in second-half
    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
 
    # Check if first-half
    # has more vowels
    if (lh > rh):
        ans += 1
 
    # Check for all possible rotations
    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 the answer
    return ans
 
# Driver code
if __name__ == "__main__":
   
    s = "abecidft"
    n = len(s)
 
    # Function call
    print(cntRotations(s, n))
 
# This code is contributed by Chitranayal

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(char[] s, int n)
    {
      int lh = 0, rh = 0, i, ans = 0;
      
      // Compute the number of
      // vowels in first-half
      for (i = 0; i < n / 2; ++i)
        if (s[i] == 'a' || s[i] == 'e' ||
            s[i] == 'i' || s[i] == 'o' ||
            s[i] == 'u')
        {
          lh++;
        }
      
      // Compute the number of
      // vowels in second-half
      for (i = n / 2; i < n; ++i)
        if (s[i] == 'a' || s[i] == 'e' ||
            s[i] == 'i' || s[i] == 'o' ||
            s[i] == 'u')
        {
          rh++;
        }
      
      // Check if first-half
      // has more vowels
      if (lh > rh)
        ans++;
      
      // Check for all possible
      // rotations
      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 the answer
      return ans;
    }
   
  // Driver code    
  static void Main()
  {
      char[] s = {'a','b','e','c',
              'i','d','f','t'};
      int n = s.Length;
        
      // Function call
      Console.WriteLine(cntRotations(s, n));
  }
}
 
// This code is contributed by divyesh072019

Java脚本


输出
4

时间复杂度: O(n)
空间复杂度: O(1)