1119. Remove Vowels from a String π
Description
Given a string s, remove the vowels 'a', 'e', 'i', 'o', and 'u' from it, and return the new string.
Example 1:
Input: s = "leetcodeisacommunityforcoders" Output: "ltcdscmmntyfrcdrs"
Example 2:
Input: s = "aeiou" Output: ""
Constraints:
1 <= s.length <= 1000sconsists of only lowercase English letters.
Solutions
Solution 1: Simulation
Thinking
Dropping vowels is a per-character test: one scan that skips aeiou builds the answer. Repeated replace calls would walk the string several times for no gain.
We can directly traverse the string according to the requirements of the problem, and append characters that are not vowels to the result string.
The time complexity is \(O(n)\), where \(n\) is the length of the string. Ignoring the space consumption of the answer, the space complexity is \(O(1)\).
1 2 3 | |
1 2 3 4 5 6 7 8 9 10 11 12 | |
1 2 3 4 5 6 7 8 9 10 11 12 | |
1 2 3 4 5 6 7 8 9 | |
1 2 3 | |