{"id":232,"date":"2021-09-01T13:06:32","date_gmt":"2021-09-01T13:06:32","guid":{"rendered":"https:\/\/funwith.dev\/?page_id=232"},"modified":"2023-07-04T16:01:30","modified_gmt":"2023-07-04T16:01:30","slug":"anagrams","status":"publish","type":"page","link":"https:\/\/funwithdev.com\/?page_id=232","title":{"rendered":"Anagrams"},"content":{"rendered":"\n<h4 class=\"wp-block-heading has-text-align-center has-primary-color has-subtle-background-background-color has-text-color has-background has-medium-font-size\">What are anagrams?<\/h4>\n\n\n\n<p>An anagram is the rearranging of letters of a word to make other words with meaning. The word itself is an example because you can rearrange the letters to spell words with one less character. The word <em>Anagram<\/em> can be rearranged into ragman or Amarna as an example but there is a missing letter. When researching this topic, I was surprised to learn how rich the history of this was and I learned that it dates back to the ancient Greeks and Romans. [<a href=\"https:\/\/funwith.dev\/wp-admin\/post.php?post=128&action=edit#masterreferences\">1<\/a>], [<a href=\"https:\/\/funwith.dev\/wp-admin\/post.php?post=128&action=edit#masterreferences\">2<\/a>].<\/p>\n\n\n\n<h4 class=\"wp-block-heading has-text-align-center has-primary-color has-subtle-background-background-color has-text-color has-background has-medium-font-size\">How is this relevant to my life?<\/h4>\n\n\n\n<p>If you are a developer you may have run into this type of interview question already. I have seen interviewers ask this one right after the palindrome question. If you are new to this field it can be intimidating if you have not prepared for a code interview like the ones I am referring to.  It is important to know how to explain what this is, how to implement it on the white board and in a virtual coding interview. Other than what I described above this is a nice to know in case the panel decides to throw it at you during the interview process. <\/p>\n\n\n\n<h4 class=\"wp-block-heading has-text-align-center has-subtle-background-background-color has-background has-medium-font-size\">Complexity Analysis<\/h4>\n\n\n\n<p>As simple as an anagram seems, we can actually analyze how long an iterative algorithm implementation would run. If you had N characters from two words you can complete it in the worst case in O(2N) by ordering the characters of each word and then comparing.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading has-text-align-center has-primary-color has-subtle-background-background-color has-text-color has-background has-medium-font-size\">Real Examples<\/h4>\n\n\n\n<p>As I was preparing for this article I found it interesting how many ways a developer could implement an algorithm to determine if two words are anagrams of each other or finding all the possible combinations for one word. The following are just a few of the ones I either figured out on my own or adapted from various sources.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">First example in C# \u2013 Determine if two words are anagrams of each other for the same count as both words.<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted has-primary-color has-subtle-background-background-color has-text-color has-background\" style=\"font-size:12px\">bool isAnagram = false;\nvar word1 = \"dog\";\nvar word2 = \"god\";\n\nisAnagram = input1.OrderBy(x => x).SequenceEqual(input2.OrderBy(x => x));\n<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Interface implementation variation 1 <\/h4>\n\n\n\n<p>This determines if two words of the same count or different count are anagrams.<\/p>\n\n\n\n<ul class=\"wp-block-social-links is-layout-flex wp-block-social-links-is-layout-flex\"><li class=\"wp-social-link wp-social-link-github  wp-block-social-link\"><a href=\"https:\/\/github.com\/rsdev30\/FunWithDev\/blob\/main\/Anagrams\/Managers\/Fun.With.Dev.Anagrams.Managers\/Strategies\/CheckAnagramVariation1.cs\" class=\"wp-block-social-link-anchor\"><svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" version=\"1.1\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" aria-hidden=\"true\" focusable=\"false\"><path d=\"M12,2C6.477,2,2,6.477,2,12c0,4.419,2.865,8.166,6.839,9.489c0.5,0.09,0.682-0.218,0.682-0.484 c0-0.236-0.009-0.866-0.014-1.699c-2.782,0.602-3.369-1.34-3.369-1.34c-0.455-1.157-1.11-1.465-1.11-1.465 c-0.909-0.62,0.069-0.608,0.069-0.608c1.004,0.071,1.532,1.03,1.532,1.03c0.891,1.529,2.341,1.089,2.91,0.833 c0.091-0.647,0.349-1.086,0.635-1.337c-2.22-0.251-4.555-1.111-4.555-4.943c0-1.091,0.39-1.984,1.03-2.682 C6.546,8.54,6.202,7.524,6.746,6.148c0,0,0.84-0.269,2.75,1.025C10.295,6.95,11.15,6.84,12,6.836 c0.85,0.004,1.705,0.114,2.504,0.336c1.909-1.294,2.748-1.025,2.748-1.025c0.546,1.376,0.202,2.394,0.1,2.646 c0.64,0.699,1.026,1.591,1.026,2.682c0,3.841-2.337,4.687-4.565,4.935c0.359,0.307,0.679,0.917,0.679,1.852 c0,1.335-0.012,2.415-0.012,2.741c0,0.269,0.18,0.579,0.688,0.481C19.138,20.161,22,16.416,22,12C22,6.477,17.523,2,12,2z\"><\/path><\/svg><span class=\"wp-block-social-link-label screen-reader-text\">GitHub<\/span><\/a><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-preformatted has-primary-color has-subtle-background-background-color has-text-color has-background\" style=\"font-size:12px\">public bool IsAnagram(string input1, string input2)\n        {\n            if(input1 == null)\n                return false;\n\n            if (input2 == null)\n                return false;\n\n            var ordered1 = input1?.OrderBy(c => c);\n            var ordered2 = input2?.OrderBy(c => c);\n\n            if (ordered1 == null)\n                return false;\n\n            if (ordered2 == null)\n                return false;\n\n            bool? isEqual = false;\n            if (input1?.Length == input2?.Length)\n            {\n                isEqual = ordered1?.SequenceEqual(ordered2.AsEnumerable());\n                return System.Convert.ToBoolean(isEqual);\n            }\n            else\n            {\n                \/\/in this case the input2 is a subset of the letters in input1.\n                if(input1?.Length > input2?.Length)\n                {\n                    ordered2?.ToList().ForEach(c =>\n                    {\n                        isEqual = ordered1.Any(x => x.Equals(c));\n                    });\n                }\n                else\n                {\n                    \/\/vice versa, input1 is a subset of input2\n                    ordered1?.ToList().ForEach(c =>\n                    {\n                        isEqual = ordered2.Any(x => x.Equals(c));\n                    });\n                }\n            }\n\n            return System.Convert.ToBoolean(isEqual);\n        }<\/pre>\n\n\n\n<h3 class=\"wp-block-heading has-text-align-center has-normal-font-size\">References<\/h3>\n\n\n\n<p id=\"anagram\">[1] Anonymous, \u201cAnagrams\u201d retrieved from <a rel=\"noreferrer noopener\" href=\"https:\/\/www.yourdictionary.com\/anagram\" data-type=\"URL\" data-id=\"https:\/\/www.yourdictionary.com\/anagram\" target=\"_blank\">https:\/\/www.yourdictionary.com\/anagram<\/a> <\/p>\n\n\n\n<p id=\"anagram\">        Accessed 4 July 2023<\/p>\n\n\n\n<p>[2] Anonymous, \u201cPalindromes\u201d retrieved from <a rel=\"noreferrer noopener\" href=\"https:\/\/www.yourdictionary.com\/palindrome\" target=\"_blank\">https:\/\/www.yourdictionary.com\/palindrome<\/a><\/p>\n\n\n\n<p>       Accessed 4 July 2023<\/p>\n\n\n\n<p>[2] Britannica, The Editors of Encyclopedia. \u201cAnagram\u201d. Encyclopedia Britannica, 23 Apr. 2020, <a href=\"https:\/\/www.britannica.com\/topic\/anagram\">https:\/\/www.britannica.com\/topic\/anagram<\/a>. Accessed 1 September 2021.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What are anagrams? An anagram is the rearranging of letters of a word to make other words with meaning. The word itself is an example because you can rearrange the letters to spell words with one less character. The word Anagram can be rearranged into ragman or Amarna as an example but there is a [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-232","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/funwithdev.com\/index.php?rest_route=\/wp\/v2\/pages\/232","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/funwithdev.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/funwithdev.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/funwithdev.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/funwithdev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=232"}],"version-history":[{"count":33,"href":"https:\/\/funwithdev.com\/index.php?rest_route=\/wp\/v2\/pages\/232\/revisions"}],"predecessor-version":[{"id":487,"href":"https:\/\/funwithdev.com\/index.php?rest_route=\/wp\/v2\/pages\/232\/revisions\/487"}],"wp:attachment":[{"href":"https:\/\/funwithdev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}