Tutte le sfide
Facile 2 risolte 66.7% di accettazione

Count Vowels

Count the vowels in a piece of text.

Cosa imparerai

  • Loop over the characters of a string
  • Test membership in a set of vowels
  • Ignore letter case

Problema

Mostra traduzione

Count how many characters in text are vowels: a, e, i, o, u in either uppercase or lowercase.

Implement count_vowels(text).

Input

text: The text to inspect (ASCII characters).

Output

Return the number of vowels.

Vincoli

0 ≤ length ≤ 10,000

Esempi

Esempio 1
Input:
text = "Hello World"

Output atteso:
expected = 3
Input:
std::string text = "Hello World";

Output atteso:
long long expected = 3;
Input:
string text = "Hello World";

Output atteso:
long expected = 3;

Spiegazione

e, o and o are vowels: 3.

Esempio 2
Input:
text = "AEIOU and y"

Output atteso:
expected = 6
Input:
std::string text = "AEIOU and y";

Output atteso:
long long expected = 6;
Input:
string text = "AEIOU and y";

Output atteso:
long expected = 6;

Spiegazione

A, E, I, O, U, a are vowels; y is not counted: 6.

Come affrontarlo

Prova prima da solo, poi apri un suggerimento alla volta.

Suggerimento 1

Scan the text once and test each character against the vowel set.

Suggerimento 2

Treat uppercase and lowercase vowels as the same kind of letter.

Suggerimento 3

An empty string should simply return zero.

Complessità attesa

O(n) time, O(1) extra space

Riferimenti

Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.

La migliore soluzione superata di ogni programmatore. Le più veloci, leggere e meglio valutate stanno in alto.

# Programmatore Linguaggio Tempo Memoria Voto dell'IA Soluzione
1 KKKaneki Ken Python 829 ms - 5/5 Vedi il codice
2 HJhettory Jurash Python 829 ms - 4/5 Vedi il codice

Impara dagli altri

Soluzioni della community

Attenzione spoiler: 2 soluzioni complete sotto Prova prima a risolverla da solo. Puoi comunque sbirciare il codice degli altri sviluppatori e le recensioni AI. Accedi e risolvi la sfida per partecipare alla discussione.