Count Vowels, Consonants, and Spaces in a String

6
1

Given a string text, count its vowels, consonants, and ordinary spaces, treating uppercase and lowercase English letters equally. Ignore other characters and return [count of vowels, count of consonants, count of spaces].

Example 1

Input: text = "Code is fun"

Output: [4, 5, 2]

Explanation: The string contains 4 vowels, 5 consonants, and 2 spaces.

Example 2

Input: text = "AI Lab"

Output: [3, 2, 2]

Explanation: The string contains 3 vowels, 2 consonants, and 2 consecutive spaces between AI and Lab.

Approach

Read the string one character at a time from the very first character to the last and count vowels, consonants, and spaces separately. The letters a, e, i, o, and u are vowels, whether uppercase or lowercase. Any other English letter is a consonant. Each space adds one to the space count.

Ignore digits, symbols, and punctuation. After checking the whole string, the three counts give the total number of vowels, consonants, and spaces.

Algorithm

  • Examine the string from left to right.

  • Count a, e, i, o, and u as vowels, including their uppercase forms.

  • Count every other English letter as a consonant.

  • Count each ordinary space (' ') separately, including consecutive spaces.

  • Ignore digits, symbols, and punctuation.

  • Return the totals in the order [vowels, consonants, spaces].

Dry Run

count vowels , consonants , and spaces in a string

count vowels , consonants , and spaces in a string

Solution

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
// Counts English vowels, consonants, and ordinary spaces.
vector<int> countVowelsConsonantsSpaces(const string& text) {
int vowels = 0;
int consonants = 0;
int spaces = 0;
// Include both letter cases without changing the input string.
const string vowelChars = "aeiouAEIOU";
// Every character contributes to at most one count.
for (char ch : text) {
if (vowelChars.find(ch) != string::npos) {
// Both uppercase and lowercase vowels are included.
vowels++;
} else if ((ch >= 'a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z')) {
// An English letter that is not a vowel is a consonant.
consonants++;
} else if (ch == ' ') {
// Count each ordinary space, including repeated spaces.
spaces++;
}
// Digits, punctuation, and other characters are ignored.
}
// Preserve the required order of the three counts.
return {vowels, consonants, spaces};
}
};
// Driver code
int main() {
string text = "Code is fun";
// Create an instance of class Solution.
Solution sol;
vector<int> result = sol.countVowelsConsonantsSpaces(text);
cout << "[" << result[0] << ", "
<< result[1] << ", "
<< result[2] << "]\n";
return 0;
}

Complexity Analysis

Time Complexity: O(N), where N is the string length. Each character is examined once, and checking membership in the fixed vowel group takes constant time.

Space Complexity: O(1) auxiliary space because only three counters and a fixed vowel group are stored. The returned array also contains only three integers.

Interview follow-up Questions

Yes, when the string contains only English letters and ordinary spaces. If digits, symbols, or punctuation are present, those ignored characters must also be excluded. For example, "ab1" has length 3, one vowel, and no spaces. Subtracting only vowels and spaces gives 2, but the actual consonant count is 1 because the digit is ignored.

StringTCS NQT

Read Similar Blogs

Comments0