簡単 0件解決済み 正答率0%

Anagram Check

Check whether two phrases use the same letters.

このチャレンジで学べること

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

問題

翻訳を表示

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.

制約

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

例

例 1
入力:
first = "Listen"
second = "Silent"

期待される出力:
expected = True
入力:
std::string first = "Listen";
std::string second = "Silent";

期待される出力:
bool expected = true;
入力:
string first = "Listen";
string second = "Silent";

期待される出力:
bool expected = true;

解説

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

例 2
入力:
first = "apple"
second = "paddle"

期待される出力:
expected = False
入力:
std::string first = "apple";
std::string second = "paddle";

期待される出力:
bool expected = false;
入力:
string first = "apple";
string second = "paddle";

期待される出力:
bool expected = false;

解説

The letters and counts differ.

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

Remove non-letters and ignore case before comparing.

ヒント 2

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

ヒント 3

Punctuation and spaces should not affect the result.

目安の計算量

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

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。