Dễ 1 đã giải Tỷ lệ chấp nhận 100.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.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

# Người giải Ngôn ngữ Thời gian Bộ nhớ AI đánh giá Lời giải
1 KKKaneki Ken Python 877 ms - 4/5 Xem code

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

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

Cảnh báo: Có 1 lời giải đầy đủ bên dưới Hãy thử tự giải trước. Bạn vẫn có thể xem mã của người khác và đánh giá AI. Đăng nhập và giải thử thách để tham gia thảo luận.