41.3k views
1 vote
Given two strings s and t, write a function to determine if t is an anagram of s. For example,

s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.
Note:You may assume the string contains only lowercase alphabets.
Follow up:What if the inputs contain unicode characters? How would you adapt your solution to such case?
*/

User Speeder
by
8.3k points

1 Answer

3 votes

Final answer:

To check if t is an anagram of s, count the frequency of each letter in both strings and compare the counts. If the frequency maps are identical, return true; otherwise, return false. For Unicode, ensure used structures support the full Unicode character set.

Step-by-step explanation:

To determine if t is an anagram of s, a function can compare the frequency of each letter in both strings, as anagrams contain the same letters in different orders.

Here's a basic approach in a pseudo-code:

Count the frequency of each letter in s and store it in a map or dictionary.

Do the same for t.

Compare if the two maps/dictionaries are identical.

If they match, return true; otherwise, return false.

For the follow-up scenario with unicode characters, the concept remains the same, but one must ensure the data structures used to count frequencies are capable of handling a full range of Unicode characters, such as using a hash map.

User Gregg Duncan
by
8.6k points