# 242. Valid Anagram 🎢

**Difficulty**: `Easy` - **Tags**: `Hash Table`, `String`, `Sorting`

[LeetCode Problem Link](https://leetcode.com/problems/valid-anagram/)

***

## Problem Statement 📜

Given two strings `s` and `t`, determine if `t` is an anagram of `s`. An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, using all the original letters exactly once.

***

## Examples 🌟

🔹 **Example 1:**

**Input:**

```plaintext
s = "anagram", t = "nagaram"
```

**Output:**

```plaintext
true
```

🔹 **Example 2:**

**Input:**

```plaintext
s = "rat", t = "car"
```

**Output:**

```plaintext
false
```

***

## Constraints ⚙️

* `1 <= s.length, t.length <= 5 * 10^4`
* `s` and `t` consist of lowercase English letters.

***

## Solution 💡

The simplest way to check if two strings are anagrams is to compare their sorted versions. Alternatively, we can use a frequency count for more efficiency.

***

### Java Solution (Sorting Approach)

```java
import java.util.Arrays;

class Solution {
    public boolean isAnagram(String s, String t) {
        if (s.length() != t.length()) return false;

        char[] sArray = s.toCharArray();
        char[] tArray = t.toCharArray();

        Arrays.sort(sArray);
        Arrays.sort(tArray);

        return Arrays.equals(sArray, tArray);
    }
}
```

***

### Java Solution (HashMap Approach)

```java
import java.util.HashMap;
import java.util.Map;

class Solution {
    public boolean isAnagram(String s, String t) {
        if (s.length() != t.length()) return false;

        Map<Character, Integer> charCount = new HashMap<>();

        for (char c : s.toCharArray()) {
            charCount.put(c, charCount.getOrDefault(c, 0) + 1);
        }

        for (char c : t.toCharArray()) {
            if (!charCount.containsKey(c) || charCount.get(c) == 0) {
                return false;
            }
            charCount.put(c, charCount.get(c) - 1);
        }

        return true;
    }
}
```

***

## Explanation of the Solution

1. **Sorting Approach**:
   * Convert both strings to character arrays.
   * Sort the arrays.
   * Compare the sorted arrays for equality.
2. **HashMap Approach**:
   * Count the frequency of each character in `s` using a `HashMap`.
   * For each character in `t`, decrement its count in the map.
   * If a character in `t` is missing or its count goes below zero, return `false`.

***

## Time Complexity ⏳

* **Sorting Approach**:
  * **O(n log n)** for sorting, where `n` is the length of the strings.
* **HashMap Approach**:
  * **O(n)** for iterating through the strings.

## Space Complexity 💾

* **Sorting Approach**:
  * **O(n)** for storing the sorted arrays.
* **HashMap Approach**:
  * **O(1)** space for the map (since there are only 26 lowercase English letters).

You can find the full solution [here](https://github.com/ChunhThanhDe/Leetcode-Top-Interview/blob/main/Topic%205%20Hashmap/042%20Valid%20Anagram/Solution.java).


---

# 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://chunhthanhde.gitbook.io/leetcode-top-interview/topic-5-hashmap/042-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.
