Skip to main content

Vowel-Consonant Score - Solution & Explanation

EasyStringSimulation6 min read
Practice this problem

Problem Statement

You are given a string s consisting of lowercase English letters, spaces, and digits.

Let v be the number of vowels in s and c be the number of consonants in s.

A vowel is one of the letters 'a', 'e', 'i', 'o', or 'u', while any other letter in the English alphabet is considered a consonant.

The score of the string s is defined as follows:

  • If c > 0, the score = floor(v / c) where floor denotes rounding down to the nearest integer.
  • Otherwise, the score = 0.

Return an integer denoting the score of the string.

 

Example 1:

Input: s = "cooear"

Output: 2

Explanation:

The string s = "cooear" contains v = 4 vowels ('o', 'o', 'e', 'a') and c = 2 consonants ('c', 'r').

The score is floor(v / c) = floor(4 / 2) = 2.

Example 2:

Input: s = "axeyizou"

Output: 1

Explanation:

The string s = "axeyizou" contains v = 5 vowels ('a', 'e', 'i', 'o', 'u') and c = 3 consonants ('x', 'y', 'z').

The score is floor(v / c) = floor(5 / 3) = 1.

Example 3:

Input: s = "au 123"

Output: 0

Explanation:

The string s = "au 123" contains no consonants (c = 0), so the score is 0.

 

Constraints:

  • 1 <= s.length <= 100
  • s consists of lowercase English letters, spaces and digits.

Approach Overview

Problem Overview: You are given a string and must compute its vowel–consonant score. Each character contributes to the score depending on whether it is a vowel or a consonant. The task is essentially a classification and counting problem over the characters of the string.

Approach 1: Brute Force Character Check (O(n * 5) time, O(1) space)

The most direct solution checks each character and compares it against every vowel individually (a, e, i, o, u). You iterate through the string one character at a time. For every character, run a small loop over the five vowels to determine if it is a vowel; otherwise treat it as a consonant and update the score accordingly. This works because the alphabet is small, but the repeated comparisons make the implementation slightly clunky. Time complexity is O(n * 5), which simplifies to O(n), and space complexity remains O(1) since only counters are stored.

Approach 2: Counting with Vowel Set (O(n) time, O(1) space)

A cleaner approach stores all vowels in a constant lookup structure such as a set or string. Iterate once over the input string and perform a membership check to determine if the current character is a vowel. If the character exists in the vowel set, update the score according to the vowel rule; otherwise treat it as a consonant. The key insight is that set membership is constant time, so each character is processed exactly once with a single lookup. This results in O(n) time and O(1) space because the vowel set contains only five characters.

This pattern appears frequently in string processing tasks where characters are classified into groups. It also demonstrates a simple simulation approach: iterate over the input and update a running state. Because the logic only requires counting and classification, the solution fits naturally into a counting pattern.

Recommended for interviews: The vowel-set counting approach is what interviewers expect. It shows you recognize constant-time lookups and clean iteration over a string. Mentioning the naive repeated comparison method can demonstrate baseline reasoning, but implementing the single-pass set-based solution shows stronger problem-solving discipline.

Solution

We iterate through the string to count the number of vowels and consonants, denoted as v and c, respectively. Finally, we calculate the score based on the problem description.

The time complexity is O(n), where n is the length of the string. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Character ComparisonO(n * 5)O(1)Good for understanding the basic classification logic without extra data structures
Counting with Vowel SetO(n)O(1)Best general solution for interview settings and production code

Video Solution

3813. Vowel-Consonant Score (Leetcode Easy) • Programming Live with Larry • 344 views views

Watch 6 more video solutions →

Frequently Asked Questions

Is Vowel-Consonant Score easy or hard?
Vowel-Consonant Score is considered an Easy-level problem. It focuses on basic string traversal, character classification, and counting. Most solutions require a single pass through the string with constant extra space.
Vowel-Consonant Score Python/Java solution
The Python or Java implementation usually stores vowels in a set such as {'a','e','i','o','u'} and loops through the string characters. Each iteration performs a membership check and updates the score variable. The logic is identical across Python, Java, C++, Go, and TypeScript.
How to solve Vowel-Consonant Score in O(n)?
Iterate through the string once and maintain a running score. Store the five vowels in a set or string for constant-time membership checks. For each character, check if it is in the vowel set and update the score accordingly; otherwise treat it as a consonant. Since every character is processed once, the complexity is O(n).
What is the best approach for Vowel-Consonant Score?
The best approach is a single-pass counting method using a constant lookup structure (such as a set) containing the vowels. Iterate through the string once and check whether each character exists in the vowel set. Update the score depending on whether the character is a vowel or consonant. This solution runs in O(n) time with O(1) space.
Is Vowel-Consonant Score asked at Google/Amazon/Meta?
Problems involving vowel and consonant classification appear frequently in interviews because they test string traversal, counting patterns, and clean conditional logic. Variations of this pattern have shown up in screening rounds at companies like Amazon and Google as warm-up string questions.
What data structure is used in Vowel-Consonant Score?
The typical implementation uses a small set or string containing the vowels for constant-time lookup. The algorithm also maintains a running integer score while iterating through the string. No additional complex data structures are required.
What is the time complexity of Vowel-Consonant Score?
The optimal solution runs in O(n) time where n is the length of the string. Each character is processed exactly once and checked against a constant-size vowel set. Space complexity is O(1) because the algorithm only stores a few counters and a fixed set of vowels.

Ready to solve this problem?

Practice Vowel-Consonant Score with our built-in code editor and test cases.

Practice on FleetCode