给定一个字符串S ,任务是检查并接受给定的字符串是否包含所有元音,即‘a’, ‘e’, ‘i’.’o’, ‘u’ 或 ‘A’, ‘E’, ‘我’,’O’,’U’ 。
例子:
Input: S = “GeeksforGeeks”
Output: Not Accepted
Since S does not contain vowels a, i and u
Input: S = “ABeeIghiObhkUul”
Output: Accepted
Since S contains all vowels a, e, i, o and u
方法:
- 散列可以很容易地解决这个问题。为此,需要创建大小为5的哈希数据结构,以便索引 0、1、2、3 和 4 表示元音 a、e、i、o 和 u。
- 创建一个布尔数组,作为哈希数据结构,检查字符串是否存在所有元音。
- 迭代由字符字符串字符,如果字符是元音然后标记存在于布尔阵列元音
- 在字符串的迭代之后,检查布尔数组中是否存在不存在的元音。
下面是上述方法的实现:
C++
// C++ implementation to check that
// a string contains all vowels
#include
using namespace std;
// Function to to check that
// a string contains all vowels
int checkIfAllVowels(string str)
{
// Hash Array of size 5
// such that the index 0, 1, 2, 3 and 4
// represent the vowels a, e, i, o and u
int hash[5] = { 0 };
// Loop the string to mark the vowels
// which are present
for (int i = 0; i < str.length(); i++) {
if (str[i] == 'A' || str[i] == 'a')
hash[0] = 1;
else if (str[i] == 'E' || str[i] == 'e')
hash[1] = 1;
else if (str[i] == 'I' || str[i] == 'i')
hash[2] = 1;
else if (str[i] == 'O' || str[i] == 'o')
hash[3] = 1;
else if (str[i] == 'U' || str[i] == 'u')
hash[4] = 1;
}
// Loop to check if there is any vowel
// which is not present in the string
for (int i = 0; i < 5; i++) {
if (hash[i] == 0) {
return 1;
}
}
return 0;
}
// Function to to check that
// a string contains all vowels
int checkIfAllVowelsArePresent(string str)
{
if (checkIfAllVowels(str))
cout << "Not Accepted\n";
else
cout << "Accepted\n";
}
// Driver Code
int main()
{
string str = "aeioubc";
checkIfAllVowelsArePresent(str);
return 0;
}
Java
// Java implementation to check that
// a String contains all vowels
class GFG
{
// Function to to check that
// a String contains all vowels
static int checkIfAllVowels(String str)
{
// Hash Array of size 5
// such that the index 0, 1, 2, 3 and 4
// represent the vowels a, e, i, o and u
int []hash = new int[5];
// Loop the String to mark the vowels
// which are present
for (int i = 0; i < str.length(); i++)
{
if (str.charAt(i) == 'A' || str.charAt(i) == 'a')
hash[0] = 1;
else if (str.charAt(i) == 'E' || str.charAt(i) == 'e')
hash[1] = 1;
else if (str.charAt(i) == 'I' || str.charAt(i) == 'i')
hash[2] = 1;
else if (str.charAt(i) == 'O' || str.charAt(i) == 'o')
hash[3] = 1;
else if (str.charAt(i) == 'U' || str.charAt(i) == 'u')
hash[4] = 1;
}
// Loop to check if there is any vowel
// which is not present in the String
for (int i = 0; i < 5; i++)
{
if (hash[i] == 0)
{
return 1;
}
}
return 0;
}
// Function to to check that
// a String contains all vowels
static void checkIfAllVowelsArePresent(String str)
{
if (checkIfAllVowels(str) == 1)
System.out.print("Not Accepted\n");
else
System.out.print("Accepted\n");
}
// Driver Code
public static void main(String[] args)
{
String str = "aeioubc";
checkIfAllVowelsArePresent(str);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation to check that
# a string contains all vowels
# Function to to check that
# a string contains all vowels
def checkIfAllVowels(string) :
# Hash Array of size 5
# such that the index 0, 1, 2, 3 and 4
# represent the vowels a, e, i, o and u
hash = [0]*5 ;
# Loop the string to mark the vowels
# which are present
for i in range(len(string)) :
if (string[i] == 'A' or string[i] == 'a') :
hash[0] = 1;
elif (string[i] == 'E' or string[i] == 'e') :
hash[1] = 1;
elif (string[i] == 'I' or string[i] == 'i') :
hash[2] = 1;
elif (string[i] == 'O' or string[i] == 'o') :
hash[3] = 1;
elif (string[i] == 'U' or string[i] == 'u') :
hash[4] = 1;
# Loop to check if there is any vowel
# which is not present in the string
for i in range(5) :
if (hash[i] == 0) :
return 1;
return 0;
# Function to to check that
# a string contains all vowels
def checkIfAllVowelsArePresent(string) :
if (checkIfAllVowels(string)) :
print("Not Accepted");
else :
print("Accepted");
# Driver Code
if __name__ == "__main__" :
string = "aeioubc";
checkIfAllVowelsArePresent(string);
# This code is contributed by AnkitRai01
C#
// C# implementation to check that
// a String contains all vowels
using System;
class GFG
{
// Function to to check that
// a String contains all vowels
static int checkIfAllVowels(String str)
{
// Hash Array of size 5
// such that the index 0, 1, 2, 3 and 4
// represent the vowels a, e, i, o and u
int []hash = new int[5];
// Loop the String to mark the vowels
// which are present
for (int i = 0; i < str.Length; i++)
{
if (str[i] == 'A' || str[i] == 'a')
hash[0] = 1;
else if (str[i] == 'E' || str[i] == 'e')
hash[1] = 1;
else if (str[i] == 'I' || str[i] == 'i')
hash[2] = 1;
else if (str[i] == 'O' || str[i] == 'o')
hash[3] = 1;
else if (str[i] == 'U' || str[i] == 'u')
hash[4] = 1;
}
// Loop to check if there is any vowel
// which is not present in the String
for (int i = 0; i < 5; i++)
{
if (hash[i] == 0)
{
return 1;
}
}
return 0;
}
// Function to to check that
// a String contains all vowels
static void checkIfAllVowelsArePresent(String str)
{
if (checkIfAllVowels(str) == 1)
Console.Write("Not Accepted\n");
else
Console.Write("Accepted\n");
}
// Driver Code
public static void Main(String[] args)
{
String str = "aeioubc";
checkIfAllVowelsArePresent(str);
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
Accepted
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。