Tous les défis
Facile 0 résolus 0 % de réussite

Contains the Character

Keep only the words that contain a given letter, ignoring case.

Ce que vous allez apprendre

  • Check whether a string contains a character
  • Compare letters while ignoring case
  • Keep matching items in their original order

Problème

Afficher la traduction

For a word game, players enter a list of words and a letter, and the game shows only the words that use that letter. Upper and lower case count as the same letter, so K matches kite and k matches Kansas.

Implement contains_character(words, letter).

Input

words: The list of words.

letter: A single English letter.

Output

Return the words that contain the letter, in their original order and spelling. Repeated words are kept as many times as they appear. If no word matches, return an empty list.

Contraintes

0 ≤ length of words ≤ 1000

Each word has 1 to 30 ASCII letters

letter is one ASCII letter

Exemples

Exemple 1
Entrée :
words = ["apple", "Berry", "kiwi", "Grape"]
letter = "r"

Sortie attendue :
expected = ["Berry", "Grape"]
Entrée :
std::vector<std::string> words = {"apple", "Berry", "kiwi", "Grape"};
std::string letter = "r";

Sortie attendue :
std::vector<std::string> expected = {"Berry", "Grape"};
Entrée :
string[] words = new string[] {"apple", "Berry", "kiwi", "Grape"};
string letter = "r";

Sortie attendue :
string[] expected = new string[] {"Berry", "Grape"};

Explication

Berry and Grape contain an r; apple and kiwi do not.

Exemple 2
Entrée :
words = ["Ohio", "Utah", "Iowa"]
letter = "O"

Sortie attendue :
expected = ["Ohio", "Iowa"]
Entrée :
std::vector<std::string> words = {"Ohio", "Utah", "Iowa"};
std::string letter = "O";

Sortie attendue :
std::vector<std::string> expected = {"Ohio", "Iowa"};
Entrée :
string[] words = new string[] {"Ohio", "Utah", "Iowa"};
string letter = "O";

Sortie attendue :
string[] expected = new string[] {"Ohio", "Iowa"};

Explication

Case is ignored, so Ohio and Iowa both match.

Comment l'aborder

Essayez d'abord seul, puis ouvrez les indices un par un.

Indice 1

Convert both the word and the letter to the same case before comparing.

Indice 2

Return the original word, not the converted copy.

Indice 3

Do not remove duplicates; just test each word on its own.

Complexité attendue

O(total letters) time, O(n) extra space

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

La meilleure solution validée de chaque codeur. Les plus rapides, les plus légères et les mieux notées passent devant.

Aucune solution validée pour l'instant. Résolvez-le et prenez la première place.

Apprenez des autres

Solutions de la communauté

Aucune solution publiée pour l'instant. Résolvez ce défi pour partager la première.