#3813

Vowel-Consonant Score

Easy
StringSimulationString ManipulationCounting Problems
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n)
O(n)
Space
O(1)
O(1)
💡

Intuition

Time O(n)Space O(1)

Count vowels and consonants in a single pass through the string for efficiency.

⚙️

Algorithm

3 steps
  1. 1Step 1: Initialize counters for vowels and consonants.
  2. 2Step 2: Iterate through the string, updating counters based on character type.
  3. 3Step 3: Return the score based on the counts.
solution.py6 lines
1def vowel_consonant_score(s):
2    v, c = 0, 0
3    for char in s:
4        if char in 'aeiou': v += 1
5        elif char.isalpha(): c += 1
6    return v // c if c > 0 else 0

Complexity note: The complexity is O(n) as we only loop through the string once.

  • 1Vowels are defined as specific characters, while consonants are all other letters.
  • 2The score calculation depends on the ratio of vowels to consonants.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.