Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
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:
c > 0, the score = floor(v / c) where floor denotes rounding down to the nearest integer.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 <= 100s consists of lowercase English letters, spaces and digits.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"cooear"
"axeyizou"
"au 123"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3813: Vowel-Consonant Score
class Solution {
public int vowelConsonantScore(String s) {
int v = 0, c = 0;
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (Character.isLetter(ch)) {
c++;
if ("aeiou".indexOf(ch) != -1) {
v++;
}
}
}
c -= v;
return c == 0 ? 0 : v / c;
}
}
// Accepted solution for LeetCode #3813: Vowel-Consonant Score
func vowelConsonantScore(s string) int {
v, c := 0, 0
for _, ch := range s {
if unicode.IsLetter(ch) {
c++
if strings.ContainsRune("aeiou", ch) {
v++
}
}
}
c -= v
if c == 0 {
return 0
}
return v / c
}
# Accepted solution for LeetCode #3813: Vowel-Consonant Score
class Solution:
def vowelConsonantScore(self, s: str) -> int:
v = c = 0
for ch in s:
if ch.isalpha():
c += 1
if ch in "aeiou":
v += 1
c -= v
return 0 if c == 0 else v // c
// Accepted solution for LeetCode #3813: Vowel-Consonant Score
fn vowel_consonant_score(s: String) -> i32 {
let (v, c) = s.chars().fold((0, 0), |(v, c), ch| match ch {
'a' | 'e' | 'i' | 'o' | 'u' => (v + 1, c),
_ => if ch.is_ascii_lowercase() {
(v, c + 1)
} else {
(v, c)
}
});
if c > 0 {
(v as f64 / c as f64).floor() as i32
} else {
0
}
}
fn main() {
let ret = vowel_consonant_score("cooear".to_string());
println!("ret={ret}");
}
#[test]
fn test() {
assert_eq!(vowel_consonant_score("i3".to_string()), 0);
assert_eq!(vowel_consonant_score("cooear".to_string()), 2);
assert_eq!(vowel_consonant_score("axeyizou".to_string()), 1);
assert_eq!(vowel_consonant_score("au 123".to_string()), 0);
}
// Accepted solution for LeetCode #3813: Vowel-Consonant Score
function vowelConsonantScore(s: string): number {
let [v, c] = [0, 0];
for (const ch of s) {
if (/[a-zA-Z]/.test(ch)) {
c++;
if ('aeiou'.includes(ch)) {
v++;
}
}
}
c -= v;
return c === 0 ? 0 : Math.floor(v / c);
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.