📌  相关文章
📜  检查所有子串是否具有至少与辅音相同的元音数量

📅  最后修改于: 2021-05-04 08:10:31             🧑  作者: Mango

给定字符串str ,任务是检查所有长度≥2的子字符串是否至少具有与辅音数量相同的元音数量。

例子:

方法:只有两种情况不满足给定条件:

  1. 当有两个连续的辅音时(在这种情况下),大小为2的子字符串可以有2个辅音且没有元音。
  2. 当元音被两个辅音包围时,在这种情况下,长度为3的子串可能带有2个辅音和1个元音。

所有其他情况将始终满足给定条件。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function that returns true
// if character ch is a vowel
bool isVowel(char ch)
{
    switch (ch) {
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
        return true;
    }
    return false;
}
  
// Compares two integers according
// to their digit sum
bool isSatisfied(string str, int n)
{
  
    // Check if there are two
    // consecutive consonants
    for (int i = 1; i < n; i++) {
        if (!isVowel(str[i])
            && !isVowel(str[i - 1])) {
            return false;
        }
    }
  
    // Check if there is any vowel
    // surrounded by two consonants
    for (int i = 1; i < n - 1; i++) {
        if (isVowel(str[i])
            && !isVowel(str[i - 1])
            && !isVowel(str[i + 1])) {
            return false;
        }
    }
  
    return true;
}
  
// Driver code
int main()
{
    string str = "acaba";
    int n = str.length();
  
    if (isSatisfied(str, n))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
  
// Function that returns true
// if character ch is a vowel
static boolean isVowel(char ch)
{
    switch (ch) 
    {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
            return true;
    }
    return false;
}
  
// Compares two integers according
// to their digit sum
static boolean isSatisfied(char[] str, int n)
{
  
    // Check if there are two
    // consecutive consonants
    for (int i = 1; i < n; i++)
    {
        if (!isVowel(str[i]) &&
            !isVowel(str[i - 1]))
        {
            return false;
        }
    }
  
    // Check if there is any vowel
    // surrounded by two consonants
    for (int i = 1; i < n - 1; i++)
    {
        if (isVowel(str[i]) && 
            !isVowel(str[i - 1]) && 
            !isVowel(str[i + 1])) 
        {
            return false;
        }
    }
    return true;
}
  
// Driver code
public static void main(String []args) 
{
    String str = "acaba";
    int n = str.length();
  
    if (isSatisfied(str.toCharArray(), n))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation of the approach
  
# Function that returns true
# if acter ch is a vowel
def isVowel(ch):
    if ch in ['i', 'a', 'e', 'o', 'u']:
        return True
    else:
        return False
  
# Compares two integers according
# to their digit sum
def isSatisfied(st, n):
      
    # Check if there are two
    # consecutive consonants
    for i in range(1, n):
        if (isVowel(st[i]) == False and 
            isVowel(st[i - 1]) == False):
            return False
              
    # Check if there is any vowel
    # surrounded by two consonants
    for i in range(1, n - 1):
        if (isVowel(st[i]) and 
            isVowel(st[i - 1]) == False and 
            isVowel(st[i + 1]) == False ):
            return False
    return True
  
# Driver code
st = "acaba"
n = len(st)
  
if (isSatisfied(st, n)):
    print("Yes")
else:
    print("No")
  
# This code is contributed by Mohit Kumar


C#
// C# implementation of the approach
using System;
      
class GFG
{
  
// Function that returns true
// if character ch is a vowel
static bool isVowel(char ch)
{
    switch (ch) 
    {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
            return true;
    }
    return false;
}
  
// Compares two integers according
// to their digit sum
static bool isSatisfied(char[] str, int n)
{
  
    // Check if there are two
    // consecutive consonants
    for (int i = 1; i < n; i++)
    {
        if (!isVowel(str[i]) &&
            !isVowel(str[i - 1]))
        {
            return false;
        }
    }
  
    // Check if there is any vowel
    // surrounded by two consonants
    for (int i = 1; i < n - 1; i++)
    {
        if (isVowel(str[i]) && 
            !isVowel(str[i - 1]) && 
            !isVowel(str[i + 1])) 
        {
            return false;
        }
    }
    return true;
}
  
// Driver code
public static void Main(String []args) 
{
    String str = "acaba";
    int n = str.Length;
  
    if (isSatisfied(str.ToCharArray(), n))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
  
// This code is contributed by Rajput-Ji


输出:
No