# 242. Valid Anagram

[242. Valid Anagram](https://leetcode.com/problems/valid-anagram/)

Anagram 有一個特性就是只要是 Anagram 每個字元的字數都會一樣多，因此一個解法是先計算出一個單字每個字元所出現的次數，接著是再見去另外一個單字，有出現的字元的字數。

如果中間有發現其他的字元，就代表不是 Anagram 。

最後還要再記得檢查，有沒有哪個字元有在第一個單字有出現，但是第二個單字沒有出現。

{% tabs %}
{% tab title="Python" %}

```python
class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        table = defaultdict(int)
        for char in s:
            table[char] += 1

        for char in t:
            if table[char] > 0:
                table[char] -= 1
            else:
                return False

        for key in table.keys():
            if table[key] != 0:
                return False

        return True
```

{% endtab %}

{% tab title="Java" %}

```java
class Solution {
    public boolean isAnagram(String s, String t) {
        final Map<String, Integer> map = new HashMap();
        for (final String c : s.split("")) {
            map.put(c, map.getOrDefault(c, 0) + 1);
        }
        
        for (final String d : t.split("")) {
            if (map.containsKey(d)) {
                map.put(d, map.get(d) - 1);
            } else {
                return false;
            }
        }
        
        for (final Map.Entry<String, Integer> entry : map.entrySet()) {
            if (entry.getValue() != 0) {
                return false;
            }
        }
        
        return true;
    }
}
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://garylai.gitbook.io/algorithm-and-data-structure/problems/hash-table/valid-anagram.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
