Einfach 2 gelöst 66.7% Erfolgsquote

Count Vowels

Count the vowels in a piece of text.

Das lernst du

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

Problem

Übersetzung anzeigen

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.

Einschränkungen

0 ≤ length ≤ 10,000

Beispiele

Beispiel 1
Eingabe:
text = "Hello World"

Erwartete Ausgabe:
expected = 3
Eingabe:
std::string text = "Hello World";

Erwartete Ausgabe:
long long expected = 3;
Eingabe:
string text = "Hello World";

Erwartete Ausgabe:
long expected = 3;

Erläuterung

e, o and o are vowels: 3.

Beispiel 2
Eingabe:
text = "AEIOU and y"

Erwartete Ausgabe:
expected = 6
Eingabe:
std::string text = "AEIOU and y";

Erwartete Ausgabe:
long long expected = 6;
Eingabe:
string text = "AEIOU and y";

Erwartete Ausgabe:
long expected = 6;

Erläuterung

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

So gehst du vor

Versuch es zuerst allein und öffne dann einen Hinweis nach dem anderen.

Hinweis 1

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

Hinweis 2

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

Hinweis 3

An empty string should simply return zero.

Erwartete Komplexität

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

Referenzen

Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.

Die beste bestandene Lösung jedes Coders. Schnellere, sparsamere und besser bewertete Lösungen stehen weiter oben.

# Coder Sprache Zeit Speicher KI-Bewertung Lösung
1 KKKaneki Ken Python 829 ms - 5/5 Code ansehen
2 HJhettory Jurash Python 829 ms - 4/5 Code ansehen

Von anderen lernen

Community-Lösungen

Achtung Spoiler: 2 vollständige Lösungen unten Versuche es zuerst selbst zu lösen. Du kannst trotzdem einen Blick auf den Code und die KI-Reviews anderer Entwickler werfen. Melde dich an und löse die Aufgabe, um an der Diskussion teilzunehmen.