LeetCode 1119: Remove Vowels from a String Solution
Master LeetCode problem 1119 (Remove Vowels from a String), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1119. Remove Vowels from a String
Problem Explanation
Explanation:
To solve this problem, we can iterate through each character in the input string and check if it is a vowel. If it is not a vowel, we append it to a new string. Finally, we return the new string without the vowels.
- We can define a set of all vowels for easy lookup.
- Iterate through each character in the input string and check if it is a vowel.
- If the character is not a vowel, append it to the result string.
- Return the result string without vowels.
Time Complexity: O(n), where n is the length of the input string.
Space Complexity: O(n), where n is the length of the input string.
:
Solution Code
class Solution {
public String removeVowels(String S) {
Set<Character> vowels = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u'));
StringBuilder result = new StringBuilder();
for (char c : S.toCharArray()) {
if (!vowels.contains(c)) {
result.append(c);
}
}
return result.toString();
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1119 (Remove Vowels from a String)?
This page provides optimized solutions for LeetCode problem 1119 (Remove Vowels from a String) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1119 (Remove Vowels from a String)?
The time complexity for LeetCode 1119 (Remove Vowels from a String) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1119 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1119 in Java, C++, or Python.