📌  相关文章
📜  检查字符串中的元音是否按字母顺序排列

📅  最后修改于: 2021-06-25 20:40:21             🧑  作者: Mango

给定字符串“ str”,任务是查找字符串中的元音是否按字母顺序排列。该字符串仅包含小写字母。

例子:

Input: str = "aabbbddeecc"
Output: Vowels are in alphabetical order
The vowel characters in the string are : a, a, e, e 
which are in sorted order.

Input: str = "aabbbdideecc"
Output: Vowels are not in alphabetical order

方法:

  • 遍历数组并找到元音字符。
  • 如果任何元音小于数组中先前出现的元音,则打印元音不是按字母顺序排列的。
  • 否则,印刷的元音字母是按字母顺序排列的。

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
 
// Function that checks whether the vowel
// characters in a string are
// in alphabetical order or not
bool areVowelsInOrder(string s)
{
    int n = s.length();
 
    // ASCII Value 64 is less than
    // all the alphabets
    // so using it as a default value
    char c = (char)64;
 
    // check if the vowels in
    // the string are sorted or not
    for (int i = 0; i < n; i++) {
        if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i'
            || s[i] == 'o' || s[i] == 'u') {
 
            // if the vowel is smaller
            // than the previous vowel
            if (s[i] < c)
                return false;
            else {
 
                // store the vowel
                c = s[i];
            }
        }
    }
    return true;
}
 
// Driver code
int main()
{
    string s = "aabbbddeecc";
 
    // check whether the vowel
    // characters in a string are
    // in alphabetical order or not
    if (areVowelsInOrder(s))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


Java
// Java implementation of above approach
 
import java.io.*;
 
class GFG {
 
    // Function that checks whether the vowel
    // characters in a string are
    // in alphabetical order or not
    static boolean areVowelsInOrder(String s)
    {
        int n = s.length();
 
        // ASCII Value 64 is less than
        // all the alphabets
        // so using it as a default value
        char c = (char)64;
 
        // check if the vowels in
        // the string are sorted or not
        for (int i = 0; i < n; i++) {
            if (s.charAt(i) == 'a' || s.charAt(i) == 'e'
                || s.charAt(i) == 'i' || s.charAt(i) == 'o'
                || s.charAt(i) == 'u') {
 
                // if the vowel is smaller than the previous
                // vowel
                if (s.charAt(i) < c)
                    return false;
                else {
 
                    // store the vowel
                    c = s.charAt(i);
                }
            }
        }
        return true;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String s = "aabbbddeecc";
 
        // check whether the vowel
        // characters in a string are
        // in alphabetical order or not
        if (areVowelsInOrder(s))
            System.out.print("Yes");
        else
            System.out.print("No");
    }
}
 
// This Code is contributed
// by anuj_67....


Python 3
# Python3 implementation of
# above approach
 
# Function that checks whether the vowel
# characters in a string are
# in alphabetical order or not
 
 
def areVowelsInOrder(s):
 
    n = len(s)
 
    # ASCII Value 64 is less than
    # all the alphabets
    # so using it as a default value
    c = chr(64)
 
    # check if the vowels in
    # the string are sorted or not
    for i in range(0, n):
 
        if (s[i] == 'a' or s[i] == 'e' or s[i] ==
                'i' or s[i] == 'o' or s[i] == 'u'):
 
            # if the vowel is smaller
            # than the previous vowel
            if s[i] < c:
                return False
            else:
 
                # store the vowel
                c = s[i]
 
    return True
 
 
# Driver code
if __name__ == "__main__":
 
    s = "aabbbddeecc"
 
    # check whether the vowel
    # characters in a string are
    # in alphabetical order or not
    if areVowelsInOrder(s):
        print("Yes")
 
    else:
        print("No")
 
# This code is contributed by
# ANKITRAI1


C#
// C# implementation of above approach
using System;
 
class GFG {
 
    // Function that checks whether the vowel
    // characters in a string are
    // in alphabetical order or not
    public static bool areVowelsInOrder(string s)
    {
        int n = s.Length;
 
        // ASCII Value 64 is less than
        // all the alphabets
        // so using it as a default value
        char c = (char)64;
 
        // check if the vowels in
        // the string are sorted or not
        for (int i = 0; i < n; i++) {
            if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i'
                || s[i] == 'o' || s[i] == 'u') {
 
                // if the vowel is smaller
                // than the previous vowel
                if (s[i] < c) {
                    return false;
                }
                else {
 
                    // store the vowel
                    c = s[i];
                }
            }
        }
        return true;
    }
 
    // Driver code
    public static void Main(string[] args)
    {
        string s = "aabbbddeecc";
 
        // check whether the vowel
        // characters in a string are
        // in alphabetical order or not
        if (areVowelsInOrder(s)) {
            Console.Write("Yes");
        }
 
        else {
            Console.Write("No");
        }
    }
}
 
// This code is contributed
// by Shrikant13


PHP


Javascript


输出:
Yes

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

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。