Check Anagram Java | Fresher Interview
Fast recall
Quick revision
Core answer
int[26] (lowercase letters), ++ for the first string, -- for the second, then verify every bucket is zero. That's O(n) time and O(1) space. Sorting both and comparing also works but is O(n log n).Whether you pick the O(n) counting approach over the O(n log n) sort, and check length first
Forgetting the early length check, or assuming only 26 letters when input may be Unicode
Use a fixed int[26] for O(1) space and mention a HashMap for arbitrary/Unicode character sets
Interview-ready guide
Interview answer
Two strings are anagrams if one is just a rearrangement of the other, meaning they have exactly the same characters with the same counts.
The very first thing I check is the lengths: if they're different, they can't possibly be anagrams, so I return false right away. For the main check, I have two approaches. The simplest to write is to sort both strings and compare them, but that's O of n log n because of the sorting.
The approach I prefer is counting frequencies, which is O of n. For lowercase English letters I use an integer array of size twenty-six. I walk through both strings together, incrementing the count for each character in the first string and decrementing for each character in the second.
If the two strings are anagrams, every increment is cancelled by a matching decrement, so at the end every bucket is zero. If any bucket is non-zero, they're not anagrams. That's linear time, and the space is constant because the array is a fixed twenty-six slots regardless of input size.
I map a character to its bucket by subtracting the character a, so a goes to zero, b to one, and so on. The main caveat I'd raise is the alphabet assumption. The int twenty-six trick only works for lowercase a to z.
If the input can include uppercase, digits, or Unicode characters, I'd switch to a HashMap from character to count, which handles any character set at the cost of a bit more overhead. I'd also clarify up front whether case and spaces should matter.
Check Anagram Java | Fresher Interview
Direct Answer
Two strings are anagrams if they contain the same characters with the same counts. The cleanest check is a frequency count: use an int[26] (lowercase letters), ++ for the first string, -- for the second, then verify every bucket is zero. That's O(n) time and O(1) space. Sorting both and comparing also works but is O(n log n).
Deep Dive & Architecture
Two strings are anagrams when they contain the same characters with the same frequencies, only in a different order. Equal length is the first quick check; strings of different lengths cannot be anagrams under the usual definition.
Worked example: listen and silent
Count every character in listen, then subtract the matching character while scanning silent. The final count for l, i, s, t, e, and n is zero, so the strings are anagrams. For listen and silence, the length check fails immediately.
For lowercase English letters, an array of 26 counters is the simplest solution. For a wider character set, use a HashMap<Character, Integer> or count Unicode code points. The important part is to state the input rules: should spaces be ignored, is matching case-sensitive, and should punctuation be removed? For example, Dormitory and dirty room match only after lowercasing and ignoring spaces.
Counting takes O(n) time. The extra space is O(1) for a fixed alphabet and O(k) for a map with k distinct characters. Sorting both strings is also correct, but it takes O(n log n) and creates sorted copies because Java strings are immutable.
Code Example
boolean isAnagram(String a, String b) {
if (a.length() != b.length()) return false; // early exit
int[] count = new int[26];
for (int i = 0; i < a.length(); i++) {
count[a.charAt(i) - 'a']++; // count first string up
count[b.charAt(i) - 'a']--; // count second string down
}
for (int c : count) if (c != 0) return false;
return true;
}
// a="listen", b="silent"
// after loop every bucket nets to 0 -> true
// a="rat", b="car": 'r','a' net 0 but 't'=+1,'c'=-1 -> false
Common Mistakes & Pitfalls
- No length check: different lengths can never be anagrams - check first to save work.
- Assuming 26 letters:
int[26]only works for lowercase a-z; use aHashMapfor mixed case, digits, or Unicode. - Sorting unnecessarily: sorting is O(n log n); counting is O(n).
- Case/space sensitivity: decide up front whether 'Listen'/'Silent' or spaces count as equal.
Check your understanding
Answer these without looking back at the article.
- What is the time complexity of common array operations in Java?
- How do you reverse a string in Java without using built-in reverse methods?
- How do you find duplicates in an array in Java?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.