
Problem Statement
In this task, you are provided with a string s
, which exclusively contains lowercase English letters. Your objective is to create a new string by removing all the vowel characters from s
. The vowels in this context are the characters 'a', 'e', 'i', 'o', and 'u'. Once these characters are removed from the string, the resultant string should be returned. This process is intended to filter out vowels and return a consonant-only version of the input string, which can be an empty string if s
originally contains only vowels.
Examples
Example 1
Input:
s = "vultrhostsdevelopers"
Output:
"vltrhstsdvlprs"
Example 2
Input:
s = "aeiou"
Output:
""
Constraints
1 <= s.length <= 1000
s
consists of only lowercase English letters.
Approach and Intuition
To solve this problem, follow these steps:
- Define the string of vowels that need to be removed:
'aeiou'
. - Traverse each character in the input string
s
. - Check if the character is not a vowel and then append it to a new string.
- Return the newly constructed string which contains only the consonants or may be empty if no consonants were present.
Key Considerations:
Edge Cases:
- If
s
is completely made up of vowels, your function should return an empty string, just like ins = "aeiou"
. - It's crucial that the function processes up to 1000 characters efficiently, as this is the upper limit for the length of
s
according to given constraints.
- If
Efficiency: The solution should traverse the string once, checking each character, resulting in a time complexity of O(n), which is efficient given the constraints.
This straightforward approach ensures that all vowels are systematically checked and removed, and guarantees the construction of the output string with only the desired characters.
Solutions
- C++
- Java
class Solution {
public:
bool checkVowel(char ch) {
// List all lowercase vowels
return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';
}
string deleteVowels(string str) {
string result;
for (char letter : str) {
if (!checkVowel(letter)) {
result += letter;
}
}
return result;
}
};
The given C++ solution consists of a class named Solution
that provides a method to remove vowels from a string. The "deleteVowels" function iterates through each character of the input string. It uses the helper function "checkVowel" to determine if a character is a vowel or not. If it's not a vowel, the character is appended to the result string. This technique ensures that the returned string contains only non-vowel characters from the original string.
- Key components of the code:
- checkVowel function: Determines if a character is a vowel (a, e, i, o, u) using a simple condition checking.
- deleteVowels function: Utilizes the checkVowel function to filter out vowels from the input string and constructs the resultant no-vowel string.
By using this approach, characters are efficiently checked and concatenated only if they are not vowels, resulting in the desired output string. This method is straightforward and effective for string manipulation that targets specific character removal based on given conditions.
class Solution {
private boolean checkVowel(char character) {
return "aeiou".indexOf(character) != -1;
}
public String deleteVowels(String input) {
StringBuilder result = new StringBuilder(input.length());
for (int index = 0; index < input.length(); index++) {
if (!checkVowel(input.charAt(index))) {
result.append(input.charAt(index));
}
}
return result.toString();
}
}
The provided Java code defines a class Solution
with methods for removing all vowels from a string. Follow these steps to understand how the solution works:
The
checkVowel
method checks if a given character is a vowel by determining if it is present in the string "aeiou". It returnstrue
if the character is a vowel, otherwisefalse
.The
deleteVowels
method accepts a stringinput
and initializes aStringBuilder
which helps in constructing the final string without vowels.Iterate through each character of the input string using a for loop.
Check each character of the string by invoking the
checkVowel
method. IfcheckVowel
returnsfalse
, the character is not a vowel and is then appended to theStringBuilder
object.Finally, the
StringBuilder
object is converted back to a string and returned, representing the input string stripped of all vowels.
This concise and efficient approach ensures that all vowels are removed from any provided string using the Java language.
No comments yet.