簡単 1件解決済み 正答率100.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用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

# コーダー 言語 時間 メモリ AI 評価 解答
1 KKKaneki Ken Python 877 ms - 4/5 コードを見る

他の人の解答から学ぶ

コミュニティの解答

ネタバレ注意:下に1件の全解答あり まずは自分で挑戦してみましょう。他の開発者のコードやAIレビューも見ることができます。 サインインして課題を解くと議論に参加できます。