Skip to content

Latest commit

History

History
67 lines (53 loc) 路 1.63 KB

1119. Remove Vowels from a String.md

File metadata and controls

67 lines (53 loc) 路 1.63 KB

1. Description

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:

  1. S consists of lowercase English letters only.
  2. 1 <= S.length <= 1000

2. Solutions

Solution 1: Language: C Space to Time, strcpy()

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;
}