Dễ 0 đã giải Tỷ lệ chấp nhận 0%

Anagram Check

Check whether two phrases use the same letters.

Bạn sẽ học được gì

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

Đề bài

Xem bản dịch

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.

Ràng buộc

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

Ví dụ

Ví dụ 1
Đầu vào:
first = "Listen"
second = "Silent"

Kết quả mong đợi:
expected = True
Đầu vào:
std::string first = "Listen";
std::string second = "Silent";

Kết quả mong đợi:
bool expected = true;
Đầu vào:
string first = "Listen";
string second = "Silent";

Kết quả mong đợi:
bool expected = true;

Giải thích

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

Ví dụ 2
Đầu vào:
first = "apple"
second = "paddle"

Kết quả mong đợi:
expected = False
Đầu vào:
std::string first = "apple";
std::string second = "paddle";

Kết quả mong đợi:
bool expected = false;
Đầu vào:
string first = "apple";
string second = "paddle";

Kết quả mong đợi:
bool expected = false;

Giải thích

The letters and counts differ.

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

Remove non-letters and ignore case before comparing.

Gợi ý 2

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

Gợi ý 3

Punctuation and spaces should not affect the result.

Độ phức tạp mong đợi

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

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.