Facile 0 risolte 0% di accettazione

Anagram Check

Check whether two phrases use the same letters.

Cosa imparerai

  • Normalize text by case and spaces
  • Compare letter counts or sorted letters
  • Choose between sorting and counting

Problema

Mostra traduzione

Two phrases are anagrams if they contain exactly the same letters the same number of times, ignoring case, spaces and punctuation.

Implement is_anagram(first, second).

Input

first: The first phrase. second: The second phrase.

Output

Return true if the phrases are anagrams.

Vincoli

0 ≤ length of each ≤ 10,000 (ASCII)

Esempi

Esempio 1
Input:
first = "Listen"
second = "Silent"

Output atteso:
expected = True
Input:
std::string first = "Listen";
std::string second = "Silent";

Output atteso:
bool expected = true;
Input:
string first = "Listen";
string second = "Silent";

Output atteso:
bool expected = true;

Spiegazione

Both use e, i, l, n, s, t once each.

Esempio 2
Input:
first = "apple"
second = "paddle"

Output atteso:
expected = False
Input:
std::string first = "apple";
std::string second = "paddle";

Output atteso:
bool expected = false;
Input:
string first = "apple";
string second = "paddle";

Output atteso:
bool expected = false;

Spiegazione

The letters and counts differ.

Come affrontarlo

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

Suggerimento 1

Remove non-letters and ignore case before comparing.

Suggerimento 2

Count character frequencies for both phrases, or sort their normalized forms.

Suggerimento 3

Punctuation and spaces should not affect the result.

Complessità attesa

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

Riferimenti

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

Impara dagli altri

Soluzioni della community

Nessuna soluzione pubblicata. Risolvi questa sfida per condividere la prima.