---
# System prepended metadata

title: LC 242. Valid Anagram
tags: [easy, leedcode, python, c++]

---

# LC 242. Valid Anagram

### [Problem link](https://leetcode.com/problems/valid-anagram/)

###### tags: `leedcode` `easy` `python` `c++`

Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an anagram of <code>s</code>, and <code>false</code> otherwise.

An  **Anagram**  is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

**Example 1:** 
```
Input: s = "anagram", t = "nagaram"
Output: true
```
**Example 2:** 
```
Input: s = "rat", t = "car"
Output: false
```

 **Constraints:** 

- <code>1 <= s.length, t.length <= 5 * 10<sup>4</sup></code>
- <code>s</code> and <code>t</code> consist of lowercase English letters.


 **Follow up:**  What if the inputs contain Unicode characters? How would you adapt your solution to such a case?
## Solution 1
#### Python
```python=
class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        return Counter(s) == Counter(t)
```
#### C++
```cpp=
class Solution {
public:
    bool isAnagram(string s, string t) {
        unordered_map<char, int> cnts;
        for (char c: s) {
            cnts[c]++;
        }
        for (char c: t) {
            cnts[c]--;
        }
        for (auto cnt: cnts) {
           if (cnt.second != 0) {
               return false;
           }
        }
        return true;
    }
};
```

>### Complexity
>|             | Time Complexity | Space Complexity |
>| ----------- | --------------- | ---------------- |
>| Solution 1  | O(n)            | O(n)             |

## Note
x
