Todos os desafios
Fácil 0 resolvidos 0% de aceitação

Contains the Character

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

O que você vai aprender

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

Problema

Ver tradução

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.

Restrições

0 ≤ length of words ≤ 1000

Each word has 1 to 30 ASCII letters

letter is one ASCII letter

Exemplos

Exemplo 1
Entrada:
words = ["apple", "Berry", "kiwi", "Grape"]
letter = "r"

Saída esperada:
expected = ["Berry", "Grape"]
Entrada:
std::vector<std::string> words = {"apple", "Berry", "kiwi", "Grape"};
std::string letter = "r";

Saída esperada:
std::vector<std::string> expected = {"Berry", "Grape"};
Entrada:
string[] words = new string[] {"apple", "Berry", "kiwi", "Grape"};
string letter = "r";

Saída esperada:
string[] expected = new string[] {"Berry", "Grape"};

Explicação

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

Exemplo 2
Entrada:
words = ["Ohio", "Utah", "Iowa"]
letter = "O"

Saída esperada:
expected = ["Ohio", "Iowa"]
Entrada:
std::vector<std::string> words = {"Ohio", "Utah", "Iowa"};
std::string letter = "O";

Saída esperada:
std::vector<std::string> expected = {"Ohio", "Iowa"};
Entrada:
string[] words = new string[] {"Ohio", "Utah", "Iowa"};
string letter = "O";

Saída esperada:
string[] expected = new string[] {"Ohio", "Iowa"};

Explicação

Case is ignored, so Ohio and Iowa both match.

Como abordar

Tente sozinho primeiro e depois abra uma dica de cada vez.

Dica 1

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

Dica 2

Return the original word, not the converted copy.

Dica 3

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

Complexidade esperada

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

Referências

Este desafio foi adaptado para o CodeUtility. Siga o enunciado e as restrições acima.

A melhor solução aprovada de cada programador. As mais rápidas, leves e mais bem avaliadas ficam no topo.

Ainda não há soluções aprovadas. Resolva e fique com o primeiro lugar.

Aprenda com outros

Soluções da comunidade

Ainda não há soluções publicadas. Resolva este desafio para compartilhar a primeira.