Given a string str and an integer X, the task is to find and print first X vowels from str. If total vowels in str is < X then print -1.
Input: str = “GeeksForGeeks”, X = 3
‘e’, ‘e’ and ‘o’ are the first three vowels in the given string.
Input: str = “softcopy”, X = 5
Total vowel count is 2 i.e ‘o’ and ‘o’
Approach: Traverse the string character by character and check if the current character is a vowel. If current character is a vowel then concatenate it to the resultant string, result. If at any point the length of the resultant string becomes X then print the string result else print -1 in the end.
Below is the implementation of the above approach:
Time Complexity: O(n) Here, n is the length of string.
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.
- Check if a string can be converted to another string by replacing vowels and consonants
- Modify the string such that it contains all vowels at least once
- Reverse vowels in a given string
- Remove consecutive vowels from string
- Longest Subsequence of a String containing only vowels
- Count the pairs of vowels in the given string
- Program to remove vowels from a String
- Program to duplicate Vowels in String
- Permutations of string such that no two vowels are adjacent
- Print reverse string after removing vowels
- Replace every vowels with lexicographically next vowel in a String
- Modify string by removing vowels in between two consonants
- Check whether the vowels in a string are in alphabetical order or not
- Replace all consonants with nearest vowels in a string
- Modify the string by swapping continuous vowels or consonants
- Program to count vowels in a string (Iterative and Recursive)
- Queries to find the count of vowels in the substrings of the given string
- Minimum cost to convert given string to consist of only vowels
- Count the number of vowels occurring in all the substrings of given string
- Remove vowels from a string stored in a Binary Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.