By Long Luo

Here shows 3 Approaches to slove this problem: HashMap, Sorting and Counting.

# HashMap

$\textit{t}$ is an anagram of $\textit{s}$ which means that the characters in both strings appear in the same kind and number of times.

We can use two $\texttt{HashMap}$ to store the characters and the number of times, then compare the keys and values.

## Analysis

• Time Complexity: $O(n)$
• Space Complexity: $O(S)$, $S = 26$.

# Sorting

$\textit{t}$ is an anagram of $\textit{s}$ is equal to “two strings sorted equal”. Therefore, we can sort the strings $\textit{s}$ and $\textit{t}$ first, then check whether the sorted strings are equal.

## Analysis

• Time Complexity: $O(nlogn)$, sorting needs $O(nlogn)$, comparing two arrays need $O(n)$, so total is $O(nlogn)$.
• Space Complexity: $O(logn)$, because sorting needs $O(logn)$ space.

# Counting

Since the string contains only $26$ lowercase letters, we can maintain a frequency array $\textit{table}$ of length $26$.

Traverse the frequency of the characters in the record string $\textit{s}$, minus $\textit{table}$ the corresponding frequency in $\textit{table}$, if $\textit{table}[i] \lt 0$, it means that $\textit{t}$ contains an extra character that is not in $\textit{s}$, just return $\textit{false}$.

## Analysis

• Time Complexity: $O(n)$
• Space Complexity: $O(S)$, $S = 26$.

All suggestions are welcome.
If you have any query or suggestion please comment below.
Please upvote👍 if you like💗 it. Thank you:-)

Explore More Leetcode Solutions. 😉😃💗