Java
Anagram
Algorithm
Coding
Programming

Anagram algorithm in java

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Two strings are anagrams when they contain the same characters with the same frequencies, only arranged in a different order. In Java, the best algorithm depends on the character set you need to support and whether you care more about implementation simplicity or raw speed.

Start with a Clear Definition

Before writing code, decide what counts as an anagram in your application:

  • should uppercase and lowercase letters be treated as the same,
  • should spaces or punctuation be ignored,
  • do you only care about English letters or full Unicode text.

Those rules change the implementation. A quick interview-style solution for lowercase English words looks different from a production-grade solution for natural language text.

Sorting Is the Simplest Approach

The easiest algorithm is to normalize both strings, sort their characters, and compare the results.

java
1import java.util.Arrays;
2
3public class AnagramsBySorting {
4    public static boolean areAnagrams(String first, String second) {
5        char[] left = normalize(first).toCharArray();
6        char[] right = normalize(second).toCharArray();
7
8        if (left.length != right.length) {
9            return false;
10        }
11
12        Arrays.sort(left);
13        Arrays.sort(right);
14        return Arrays.equals(left, right);
15    }
16
17    private static String normalize(String value) {
18        return value
19            .toLowerCase()
20            .replaceAll("[^a-z]", "");
21    }
22
23    public static void main(String[] args) {
24        System.out.println(areAnagrams("Listen", "Silent"));   // true
25        System.out.println(areAnagrams("Triangle", "Integral")); // true
26        System.out.println(areAnagrams("Apple", "Plead"));     // false
27    }
28}

This approach is easy to explain and works well for moderate input sizes. Its time complexity is dominated by sorting, so it is roughly O(n log n).

Counting Characters Is Faster

If you know the input is restricted to lowercase English letters, a frequency array is usually the better algorithm. It runs in linear time because it only counts characters.

java
1public class AnagramsByCounting {
2    public static boolean areAnagrams(String first, String second) {
3        String left = normalize(first);
4        String right = normalize(second);
5
6        if (left.length() != right.length()) {
7            return false;
8        }
9
10        int[] counts = new int[26];
11
12        for (int i = 0; i < left.length(); i++) {
13            counts[left.charAt(i) - 'a']++;
14            counts[right.charAt(i) - 'a']--;
15        }
16
17        for (int count : counts) {
18            if (count != 0) {
19                return false;
20            }
21        }
22
23        return true;
24    }
25
26    private static String normalize(String value) {
27        return value
28            .toLowerCase()
29            .replaceAll("[^a-z]", "");
30    }
31
32    public static void main(String[] args) {
33        System.out.println(areAnagrams("Dormitory", "Dirty room"));
34        System.out.println(areAnagrams("School master", "The classroom"));
35    }
36}

The advantage here is speed and low memory overhead. The drawback is that the code assumes a narrow alphabet.

Use a Map for Broader Character Support

If your input may contain accented letters or a wider character set, a map-based solution is more flexible than a fixed array.

java
1import java.util.HashMap;
2import java.util.Map;
3
4public class UnicodeAnagrams {
5    public static boolean areAnagrams(String first, String second) {
6        String left = first.replaceAll("\\s+", "").toLowerCase();
7        String right = second.replaceAll("\\s+", "").toLowerCase();
8
9        if (left.length() != right.length()) {
10            return false;
11        }
12
13        Map<Character, Integer> counts = new HashMap<>();
14
15        for (char ch : left.toCharArray()) {
16            counts.put(ch, counts.getOrDefault(ch, 0) + 1);
17        }
18
19        for (char ch : right.toCharArray()) {
20            Integer current = counts.get(ch);
21            if (current == null) {
22                return false;
23            }
24
25            if (current == 1) {
26                counts.remove(ch);
27            } else {
28                counts.put(ch, current - 1);
29            }
30        }
31
32        return counts.isEmpty();
33    }
34}

This version handles a broader range of text, though the constant factors are higher than the simple array solution.

Choosing the Right Algorithm

A good rule of thumb is:

  • sorting for clarity and interviews,
  • counting arrays for fixed alphabets and maximum speed,
  • maps when the input domain is wider or less predictable.

The strings themselves are usually small, so readability often matters more than squeezing out tiny performance gains. Still, it is useful to know why the counting approach is considered the optimal classic answer.

Common Pitfalls

  • Forgetting to normalize case, spaces, or punctuation before comparing strings.
  • Using the array-counting approach when the input can contain characters outside a through z.
  • Sorting strings first and assuming that is always the fastest method.
  • Comparing string references instead of character content in custom implementations.
  • Ignoring the exact problem definition, which can change whether "Dormitory" and "Dirty room" should count as anagrams.

Summary

  • Anagram checking is really a character-frequency comparison problem.
  • Sorting is the simplest Java solution and runs in roughly O(n log n).
  • Counting characters with an array gives linear-time performance for fixed alphabets.
  • A Map is the better option when input may include a broader character set.
  • The best algorithm depends on your normalization rules and the kinds of strings your program must support.

Course illustration
Course illustration

All Rights Reserved.