Chaturmind
LearnDSASystem DesignBlogPremium
Sign inGet started
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML

Company

  • Blog
  • Premium
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.

DSA›Strings›Valid Anagram
EasyStrings

Valid Anagram

stringhash-mapsorting

Problem

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

Examples

Example 1

Input: s = "anagram", t = "nagaram"

Output: true

Example 2

Input: s = "rat", t = "car"

Output: false

Constraints

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

Hints

Hint 1

Count character frequencies with an array of size 26, or use a hash map.

Solutions

public boolean isAnagram(String s, String t) {
    if (s.length() != t.length()) return false;
    int[] count = new int[26];
    for (char c : s.toCharArray()) count[c - 'a']++;
    for (char c : t.toCharArray()) count[c - 'a']--;
    for (int v : count) if (v != 0) return false;
    return true;
}
Java

Time: O(n) · Space: O(1)