Opening the reading…
Opening the reading…
DSA FUNDAMENTALS › STRING BASICS
The vowels are 'a', 'e', 'i', 'o', and 'u', in either uppercase or lowercase. A different letter is a consonant. This creates two tests for a consonant: the character must first be a letter, and it must then fail the vowel test. A space, number, or special character fails the first test, so it belongs to neither count.
In "LearnYard 2024!", the letters 'e', 'a', and 'a' are vowels. The letters 'L', 'r', 'n', 'Y', 'r', and 'd' are consonants. The space, the digits '2', '0', '2', and '4', and the exclamation mark are not letters, so they are ignored.
Where does the character '2' from "LearnYard 2024!" belong?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Start vowelCount and consonantCount at 0, then inspect each character once. A vowel increases vowelCount. A letter that is not a vowel increases consonantCount. Every other character leaves both counters unchanged.
| INDEX | CHARACTER | CLASSIFICATION | VOWELCOUNT | CONSONANTCOUNT |
|---|---|---|---|---|
| 0 | 'L' | consonant | 0 | 1 |
| 1 | 'e' | vowel | 1 | 1 |
| 2 | 'a' | vowel | 2 | 1 |
| 3 | 'r' | consonant | 2 | 2 |
| 4 | 'n' | consonant | 2 | 3 |
| 5 | 'Y' | consonant | 2 | 4 |
| 6 | 'a' | vowel | 3 | 4 |
| 7 | 'r' | consonant | 3 | 5 |
| 8 | 'd' | consonant | 3 | 6 |
| 9 | ' ' | ignored | 3 | 6 |
| 10 | '2' | ignored | 3 | 6 |
| 11 | '0' | ignored | 3 | 6 |
| 12 | '2' | ignored | 3 | 6 |
| 13 | '4' | ignored | 3 | 6 |
| 14 | '!' | ignored | 3 | 6 |
The scan ends with vowelCount = 3 and consonantCount = 6. Notice that each letter increments exactly one counter, while the five non-letters after index 8 do not change either counter. The uppercase 'Y' is counted as a consonant here because it is a letter and is not one of the five vowels.
The requested difference is consonants minus vowels, so use consonantCount - vowelCount. For this string, that is 6 - 3 = 3. The result is signed: a negative result would mean the string had more vowels than consonants.
Reversing the operands gives 3 - 6 = -3, which answers the reverse question, vowels minus consonants. Taking an absolute value would hide which count is larger, so do not replace the requested subtraction with an absolute difference.
Using the final counters for "LearnYard 2024!", type the exact subtraction and result for consonants minus vowels.
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Both languages should test for a vowel first, then test whether a non-vowel is a letter before increasing consonantCount. If the second branch checks only that the character is not a vowel, the space, digits, and exclamation mark will be counted as consonants, producing the wrong result.
int vowelCount = 0;
int consonantCount = 0;
for (char c : text) {
if (isVowel(c)) {
vowelCount++;
} else if ((c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z')) {
consonantCount++;
}
}
int difference = consonantCount - vowelCount;int vowelCount = 0;
int consonantCount = 0;
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (isVowel(c)) {
vowelCount++;
} else if ((c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z')) {
consonantCount++;
}
}
int difference = consonantCount - vowelCount;The scan checks each of the 15 characters once, so its time complexity is O(n), where n is the string length. It uses only vowelCount, consonantCount, and a few temporary values, so its extra space complexity is O(1).