Đề bài
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ụ
Đầ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.
Đầ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.