{"id":454,"date":"2016-11-28T11:58:33","date_gmt":"2016-11-28T16:58:33","guid":{"rendered":"http:\/\/www.ferociouscoder.com\/blog\/?p=454"},"modified":"2016-11-28T12:16:13","modified_gmt":"2016-11-28T17:16:13","slug":"hackerrank-cracking-coding-interview-strings-making-anagrams","status":"publish","type":"post","link":"https:\/\/www.ferociouscoder.com\/blog\/archives\/hackerrank-cracking-coding-interview-strings-making-anagrams-454.html","title":{"rendered":"Hackerrank: Cracking the Coding Interview &#8211; Strings: Making Anagrams"},"content":{"rendered":"<p>The solution to this problem involves figuring out that if we just take the differences in the counts of the number of distinct characters in each string then that is the optimal amount of deletions we need to make. It should also be noted that while doing the calculations we need to ignore negative values and make them positive instead.<\/p>\n<p>Problem: <a href=\"https:\/\/www.hackerrank.com\/challenges\/ctci-making-anagrams\" target=\"_blank\">https:\/\/www.hackerrank.com\/challenges\/ctci-making-anagrams<\/a><\/p>\n<p>Solution:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.io.*;\r\nimport java.util.*;\r\nimport java.text.*;\r\nimport java.math.*;\r\nimport java.util.regex.*;\r\npublic class Solution {\r\n    public static int numberNeeded(String first, String second) {\r\n        char&#x5B;] a = first.toCharArray();\r\n        char&#x5B;] b = second.toCharArray();\r\n        int&#x5B;] ac = new int&#x5B;26];\r\n        int&#x5B;] bc = new int&#x5B;26];\r\n        for (int i=0; i&lt;a.length; i++) {\r\n            ac&#x5B;a&#x5B;i]-'a']++;\r\n        }\r\n        for (int i=0; i&lt;b.length; i++) {\r\n            bc&#x5B;b&#x5B;i]-'a']++;\r\n        }\r\n        int ans = 0;\r\n        for (int i=0; i&lt;ac.length; i++) {\r\n            ans += Math.abs(ac&#x5B;i] - bc&#x5B;i]);\r\n        }\r\n        return ans;\r\n    }\r\n  \r\n    public static void main(String&#x5B;] args) {\r\n        Scanner in = new Scanner(System.in);\r\n        String a = in.next();\r\n        String b = in.next();\r\n        System.out.println(numberNeeded(a, b));\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The solution to this problem involves figuring out that if we just take the differences in the counts of the number of distinct characters in each string then that is the optimal amount of deletions we need to make. It should also be noted that while doing the calculations we need to ignore negative values &hellip; <a href=\"https:\/\/www.ferociouscoder.com\/blog\/archives\/hackerrank-cracking-coding-interview-strings-making-anagrams-454.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Hackerrank: Cracking the Coding Interview &#8211; Strings: Making Anagrams<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[28,29,19],"tags":[33,32,20],"class_list":["post-454","post","type-post","status-publish","format-standard","hentry","category-cracking-the-coding-interview","category-hackerrank","category-java","tag-ctci","tag-hackerrank","tag-java-2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Hackerrank: Cracking the Coding Interview - Strings: Making Anagrams - Ferocious Coder<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.ferociouscoder.com\/blog\/archives\/hackerrank-cracking-coding-interview-strings-making-anagrams-454.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Hackerrank: Cracking the Coding Interview - Strings: Making Anagrams - Ferocious Coder\" \/>\n<meta property=\"og:description\" content=\"The solution to this problem involves figuring out that if we just take the differences in the counts of the number of distinct characters in each string then that is the optimal amount of deletions we need to make. It should also be noted that while doing the calculations we need to ignore negative values &hellip; Continue reading Hackerrank: Cracking the Coding Interview &#8211; Strings: Making Anagrams &rarr;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.ferociouscoder.com\/blog\/archives\/hackerrank-cracking-coding-interview-strings-making-anagrams-454.html\" \/>\n<meta property=\"og:site_name\" content=\"Ferocious Coder\" \/>\n<meta property=\"article:published_time\" content=\"2016-11-28T16:58:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2016-11-28T17:16:13+00:00\" \/>\n<meta name=\"author\" content=\"Rawrosaur\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rawrosaur\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/hackerrank-cracking-coding-interview-strings-making-anagrams-454.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/hackerrank-cracking-coding-interview-strings-making-anagrams-454.html\"},\"author\":{\"name\":\"Rawrosaur\",\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#\\\/schema\\\/person\\\/1fb5cbee546cffd619a7b301e3dc447a\"},\"headline\":\"Hackerrank: Cracking the Coding Interview &#8211; Strings: Making Anagrams\",\"datePublished\":\"2016-11-28T16:58:33+00:00\",\"dateModified\":\"2016-11-28T17:16:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/hackerrank-cracking-coding-interview-strings-making-anagrams-454.html\"},\"wordCount\":196,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#\\\/schema\\\/person\\\/1fb5cbee546cffd619a7b301e3dc447a\"},\"keywords\":[\"ctci\",\"hackerrank\",\"java\"],\"articleSection\":[\"Cracking the Coding Interview\",\"HackerRank\",\"Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/hackerrank-cracking-coding-interview-strings-making-anagrams-454.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/hackerrank-cracking-coding-interview-strings-making-anagrams-454.html\",\"url\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/hackerrank-cracking-coding-interview-strings-making-anagrams-454.html\",\"name\":\"Hackerrank: Cracking the Coding Interview - Strings: Making Anagrams - Ferocious Coder\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#website\"},\"datePublished\":\"2016-11-28T16:58:33+00:00\",\"dateModified\":\"2016-11-28T17:16:13+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/hackerrank-cracking-coding-interview-strings-making-anagrams-454.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/hackerrank-cracking-coding-interview-strings-making-anagrams-454.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/hackerrank-cracking-coding-interview-strings-making-anagrams-454.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Hackerrank: Cracking the Coding Interview &#8211; Strings: Making Anagrams\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/\",\"name\":\"Ferocious Coder\",\"description\":\"RAWR!\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#\\\/schema\\\/person\\\/1fb5cbee546cffd619a7b301e3dc447a\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#\\\/schema\\\/person\\\/1fb5cbee546cffd619a7b301e3dc447a\",\"name\":\"Rawrosaur\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg\",\"caption\":\"Rawrosaur\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg\"},\"sameAs\":[\"http:\\\/\\\/www.ferociouscoder.com\\\/\"],\"url\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/author\\\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Hackerrank: Cracking the Coding Interview - Strings: Making Anagrams - Ferocious Coder","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.ferociouscoder.com\/blog\/archives\/hackerrank-cracking-coding-interview-strings-making-anagrams-454.html","og_locale":"en_US","og_type":"article","og_title":"Hackerrank: Cracking the Coding Interview - Strings: Making Anagrams - Ferocious Coder","og_description":"The solution to this problem involves figuring out that if we just take the differences in the counts of the number of distinct characters in each string then that is the optimal amount of deletions we need to make. It should also be noted that while doing the calculations we need to ignore negative values &hellip; Continue reading Hackerrank: Cracking the Coding Interview &#8211; Strings: Making Anagrams &rarr;","og_url":"https:\/\/www.ferociouscoder.com\/blog\/archives\/hackerrank-cracking-coding-interview-strings-making-anagrams-454.html","og_site_name":"Ferocious Coder","article_published_time":"2016-11-28T16:58:33+00:00","article_modified_time":"2016-11-28T17:16:13+00:00","author":"Rawrosaur","twitter_misc":{"Written by":"Rawrosaur","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/hackerrank-cracking-coding-interview-strings-making-anagrams-454.html#article","isPartOf":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/hackerrank-cracking-coding-interview-strings-making-anagrams-454.html"},"author":{"name":"Rawrosaur","@id":"https:\/\/www.ferociouscoder.com\/blog\/#\/schema\/person\/1fb5cbee546cffd619a7b301e3dc447a"},"headline":"Hackerrank: Cracking the Coding Interview &#8211; Strings: Making Anagrams","datePublished":"2016-11-28T16:58:33+00:00","dateModified":"2016-11-28T17:16:13+00:00","mainEntityOfPage":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/hackerrank-cracking-coding-interview-strings-making-anagrams-454.html"},"wordCount":196,"commentCount":0,"publisher":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/#\/schema\/person\/1fb5cbee546cffd619a7b301e3dc447a"},"keywords":["ctci","hackerrank","java"],"articleSection":["Cracking the Coding Interview","HackerRank","Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.ferociouscoder.com\/blog\/archives\/hackerrank-cracking-coding-interview-strings-making-anagrams-454.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/hackerrank-cracking-coding-interview-strings-making-anagrams-454.html","url":"https:\/\/www.ferociouscoder.com\/blog\/archives\/hackerrank-cracking-coding-interview-strings-making-anagrams-454.html","name":"Hackerrank: Cracking the Coding Interview - Strings: Making Anagrams - Ferocious Coder","isPartOf":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/#website"},"datePublished":"2016-11-28T16:58:33+00:00","dateModified":"2016-11-28T17:16:13+00:00","breadcrumb":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/hackerrank-cracking-coding-interview-strings-making-anagrams-454.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.ferociouscoder.com\/blog\/archives\/hackerrank-cracking-coding-interview-strings-making-anagrams-454.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/hackerrank-cracking-coding-interview-strings-making-anagrams-454.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.ferociouscoder.com\/blog"},{"@type":"ListItem","position":2,"name":"Hackerrank: Cracking the Coding Interview &#8211; Strings: Making Anagrams"}]},{"@type":"WebSite","@id":"https:\/\/www.ferociouscoder.com\/blog\/#website","url":"https:\/\/www.ferociouscoder.com\/blog\/","name":"Ferocious Coder","description":"RAWR!","publisher":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/#\/schema\/person\/1fb5cbee546cffd619a7b301e3dc447a"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.ferociouscoder.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.ferociouscoder.com\/blog\/#\/schema\/person\/1fb5cbee546cffd619a7b301e3dc447a","name":"Rawrosaur","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg","caption":"Rawrosaur"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg"},"sameAs":["http:\/\/www.ferociouscoder.com\/"],"url":"https:\/\/www.ferociouscoder.com\/blog\/archives\/author\/admin"}]}},"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1mYMV-7k","jetpack_likes_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/posts\/454","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/comments?post=454"}],"version-history":[{"count":1,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/posts\/454\/revisions"}],"predecessor-version":[{"id":455,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/posts\/454\/revisions\/455"}],"wp:attachment":[{"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/media?parent=454"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/categories?post=454"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/tags?post=454"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}