Tutte le sfide
Facile 1 risolta 100.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.

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 877 ms - 4/5 Vedi il codice

Impara dagli altri

Soluzioni della community

Attenzione spoiler: 1 soluzione completa 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.