{"id":1036,"date":"2026-10-30T12:16:04","date_gmt":"2026-10-30T12:16:04","guid":{"rendered":"https:\/\/blog.debuggersspace.com\/?p=1036"},"modified":"2026-10-30T14:12:02","modified_gmt":"2026-10-30T14:12:02","slug":"check-whether-two-strings-are-anagrams","status":"publish","type":"post","link":"https:\/\/blog.debuggersspace.com\/index.php\/2026\/10\/30\/check-whether-two-strings-are-anagrams\/","title":{"rendered":"Check whether two strings are anagrams."},"content":{"rendered":"<div class='booster-block booster-read-block'>\n                <div class=\"twp-read-time\">\n                \t<i class=\"booster-icon twp-clock\"><\/i> <span>Read Time:<\/span>1 Minute, 40 Second                <\/div>\n\n            <\/div>\n<p class=\"wp-block-paragraph\">Two strings are anagrams if they contain the same characters the same number of times, in any order: &#8220;listen&#8221; and &#8220;silent&#8221;, &#8220;debit card&#8221; and &#8220;bad credit&#8221; (ignoring spaces).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Counting array (O(n), the answer to give)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>static boolean isAnagram(String a, String b) {\n    if (a.length() != b.length()) return false;\n    int[] count = new int[26];\n    for (int i = 0; i &lt; a.length(); i++) {\n        count[a.charAt(i) - 'a']++;\n        count[b.charAt(i) - 'a']--;\n    }\n    for (int c : count) if (c != 0) return false;\n    return true;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">One pass increments for the first string and decrements for the second. If every count is back to zero, they are anagrams. Time O(n), space O(1) because the array size is fixed at 26. This assumes lowercase a to z; say that assumption out loud.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Sorting (simple, O(n log n))<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>static boolean isAnagramSort(String a, String b) {\n    char[] x = a.toCharArray(), y = b.toCharArray();\n    Arrays.sort(x);\n    Arrays.sort(y);\n    return Arrays.equals(x, y);\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Any characters, ignoring case and spaces<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>static boolean isAnagramAny(String a, String b) {\n    Map&lt;Integer, Integer&gt; m = new HashMap&lt;&gt;();\n    a.toLowerCase().codePoints().filter(c -&gt; c != ' ').forEach(c -&gt; m.merge(c, 1, Integer::sum));\n    b.toLowerCase().codePoints().filter(c -&gt; c != ' ').forEach(c -&gt; m.merge(c, -1, Integer::sum));\n    return m.values().stream().allMatch(v -&gt; v == 0);\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Mistakes to avoid<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Checking only that each character of one string appears in the other: &#8220;aab&#8221; and &#8220;abb&#8221; would pass.<\/li>\n\n\n<li>Forgetting the length check, which is the cheapest early exit.<\/li>\n\n\n<li>Not clarifying case, spaces and character set before coding.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Follow-up<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Group a list of words into anagram groups: use the sorted word (or the 26-count signature) as a <code>HashMap<\/code> key.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>This answer was written by Debugger&#8217;s Space as practice material. It is asked in most Indian IT services interviews (TCS, Infosys, Wipro, HCLTech, Tech Mahindra, LTIMindtree, Accenture, Cognizant, Capgemini, IBM). See each company&#8217;s hiring steps and practice questions on <a href=\"https:\/\/www.debuggersspace.com\/companies\">Debugger&#8217;s Space<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Check whether two strings are anagrams with a 26-count array in O(n), the sorting approach, a HashMap for Unicode, and how to ignore spaces and case.<\/p>\n","protected":false},"author":43,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","footnotes":""},"categories":[263],"tags":[234,236,270,235,275,231,237,229,264,233,288,228,232,230],"class_list":["post-1036","post","type-post","status-publish","format-standard","hentry","category-common-interview-questions","tag-accenture","tag-capgemini","tag-coding-interview-questions","tag-cognizant","tag-freshers","tag-hcltech","tag-ibm","tag-infosys","tag-java-interview-questions","tag-ltimindtree","tag-strings","tag-tcs","tag-tech-mahindra","tag-wipro"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Himanshu Namdeo","author_link":"https:\/\/blog.debuggersspace.com\/author\/admin\/"},"uagb_comment_info":0,"uagb_excerpt":"Check whether two strings are anagrams with a 26-count array in O(n), the sorting approach, a HashMap for Unicode, and how to ignore spaces and case.","_links":{"self":[{"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/1036","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/users\/43"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/comments?post=1036"}],"version-history":[{"count":1,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/1036\/revisions"}],"predecessor-version":[{"id":1077,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/1036\/revisions\/1077"}],"wp:attachment":[{"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/media?parent=1036"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/categories?post=1036"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/tags?post=1036"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}