ง่าย 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

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 กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก