Given a string S
, remove the vowels 'a'
, 'e'
, 'i'
, 'o'
, and 'u'
from it, and return the new string.
Example 1:
Input: "leetcodeisacommunityforcoders"
Output: "ltcdscmmntyfrcdrs"
Example 2:
Input: "aeiou"
Output: ""
Note:
S
consists of lowercase English letters only.1 <= S.length <= 1000
Create another empty array and copy all non-vowel characters to the new string.
- Thursday, 27 August, 2020
- Time Complexity:
$O(n)$ - Space Complexity:
$O(n)$ - Runtime: 0 ms, faster than 100.00% of C online submissions for Remove Vowels from a String.
- Memory Usage: 5.8 MB, less than 37.50% of C online submissions for Remove Vowels from a String.
// Helper function to determine whether a character is a lower case vowel.
bool isVowel(char ch) {
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
return true;
} else {
return false;
}
}
char *removeVowels(char *S){
int len = strlen(S);
int i,j = 0;
// Use a buffer array to store the new string.
// Passing the string one by one, and checking if the character is a vowel.
// If no, copy it to the new array.
char noVowel[len];
while (i < len) {
if (isVowel(S[i]) != true) {
noVowel[j] = S[i];
j++;
}
i++;
}
// Copy the buffer array into S, and then return S.
noVowel[j] = '\0';
strcpy(S, noVowel);
return S;
}