[{"data":1,"prerenderedAt":512},["ShallowReactive",2],{"i-lucide:sun-moon":3,"i-lucide:menu":8,"post-\u002Fblog\u002F2023-01-14-anagram":10,"surround-\u002Fblog\u002F2023-01-14-anagram":495,"i-lucide:arrow-left":506,"i-lucide:sparkles":508,"i-lucide:user":510},{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":7},0,24,false,"\u003Cpath fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M12 2v2m2.837 12.385a6 6 0 1 1-7.223-7.222c.624-.147.97.66.715 1.248a4 4 0 0 0 5.26 5.259c.589-.255 1.396.09 1.248.715M16 12a4 4 0 0 0-4-4m7-3l-1.256 1.256M20 12h2\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":9},"\u003Cpath fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M4 5h16M4 12h16M4 19h16\"\u002F>",{"id":11,"title":12,"authors":13,"body":16,"date":483,"description":22,"extension":484,"meta":485,"navigation":231,"path":488,"seo":489,"stem":490,"tags":491,"toc":6,"__hash__":494},"blog\u002Fblog\u002F2023-01-14-anagram.md","Is a string an anagram?",[14,15],"gpt","pfarmer",{"type":17,"value":18,"toc":479},"minimark",[19,23,28,31,181,184,187,190,194,197,463,466,469,472,475],[20,21,22],"p",{},"An anagram is a word or phrase formed by rearranging the letters of another word or phrase. For example, \"elbow\" and \"below\" are anagrams of each other. In coding interviews and programming challenges, you may be asked to write a function that checks if two input strings are anagrams. Here's how to do this in both Python and JavaScript.",[24,25,27],"h2",{"id":26},"python-solution","Python Solution",[20,29,30],{},"Here is one way to check if two strings are anagrams in Python:",[32,33,38],"pre",{"className":34,"code":35,"language":36,"meta":37,"style":37},"language-python shiki shiki-themes github-light github-dark-default","def are_anagrams(str1, str2):\n  # Convert both strings to lowercase and remove whitespace\n  str1 = str1.replace(\" \", \"\").lower()\n  str2 = str2.replace(\" \", \"\").lower()\n  \n  # Check if lengths are different\n  if len(str1) != len(str2):\n    return False\n  \n  # Sort the strings and compare\n  return sorted(str1) == sorted(str2)\n","python","",[39,40,41,58,65,90,109,115,121,142,151,156,162],"code",{"__ignoreMap":37},[42,43,46,50,54],"span",{"class":44,"line":45},"line",1,[42,47,49],{"class":48},"sDgYj","def",[42,51,53],{"class":52},"sVjZL"," are_anagrams",[42,55,57],{"class":56},"szmH1","(str1, str2):\n",[42,59,61],{"class":44,"line":60},2,[42,62,64],{"class":63},"sPyeA","  # Convert both strings to lowercase and remove whitespace\n",[42,66,68,71,74,77,81,84,87],{"class":44,"line":67},3,[42,69,70],{"class":56},"  str1 ",[42,72,73],{"class":48},"=",[42,75,76],{"class":56}," str1.replace(",[42,78,80],{"class":79},"sNbzA","\" \"",[42,82,83],{"class":56},", ",[42,85,86],{"class":79},"\"\"",[42,88,89],{"class":56},").lower()\n",[42,91,93,96,98,101,103,105,107],{"class":44,"line":92},4,[42,94,95],{"class":56},"  str2 ",[42,97,73],{"class":48},[42,99,100],{"class":56}," str2.replace(",[42,102,80],{"class":79},[42,104,83],{"class":56},[42,106,86],{"class":79},[42,108,89],{"class":56},[42,110,112],{"class":44,"line":111},5,[42,113,114],{"class":56},"  \n",[42,116,118],{"class":44,"line":117},6,[42,119,120],{"class":63},"  # Check if lengths are different\n",[42,122,124,127,131,134,137,139],{"class":44,"line":123},7,[42,125,126],{"class":48},"  if",[42,128,130],{"class":129},"sPgVT"," len",[42,132,133],{"class":56},"(str1) ",[42,135,136],{"class":48},"!=",[42,138,130],{"class":129},[42,140,141],{"class":56},"(str2):\n",[42,143,145,148],{"class":44,"line":144},8,[42,146,147],{"class":48},"    return",[42,149,150],{"class":129}," False\n",[42,152,154],{"class":44,"line":153},9,[42,155,114],{"class":56},[42,157,159],{"class":44,"line":158},10,[42,160,161],{"class":63},"  # Sort the strings and compare\n",[42,163,165,168,171,173,176,178],{"class":44,"line":164},11,[42,166,167],{"class":48},"  return",[42,169,170],{"class":129}," sorted",[42,172,133],{"class":56},[42,174,175],{"class":48},"==",[42,177,170],{"class":129},[42,179,180],{"class":56},"(str2)\n",[20,182,183],{},"This works by first cleaning up the input strings - converting them to lowercase and removing any whitespace.",[20,185,186],{},"We then check if the lengths are different - if so, they cannot be anagrams.",[20,188,189],{},"Finally, we sort each string alphabetically and compare the sorted versions. If they are equal, the original strings must have been anagrams.",[24,191,193],{"id":192},"javascript-solution","JavaScript Solution",[20,195,196],{},"Similarly in JavaScript:",[32,198,202],{"className":199,"code":200,"language":201,"meta":37,"style":37},"language-js shiki shiki-themes github-light github-dark-default","function areAnagrams(str1, str2) {\n\n  \u002F\u002F Remove whitespace and lowercase\n  let s1 = str1.replace(\u002F\\s\u002Fg, '').toLowerCase();\n  let s2 = str2.replace(\u002F\\s\u002Fg, '').toLowerCase();  \n\n  \u002F\u002F Check length\n  if (s1.length !== s2.length) return false;\n\n  \u002F\u002F Sort and compare\n  let sorted1 = s1.split('').sort().join(''); \n  let sorted2 = s2.split('').sort().join('');\n\n  return sorted1 === sorted2;\n\n}\n","js",[39,203,204,227,233,238,281,316,320,325,355,359,364,401,434,439,452,457],{"__ignoreMap":37},[42,205,206,209,212,215,219,221,224],{"class":44,"line":45},[42,207,208],{"class":48},"function",[42,210,211],{"class":52}," areAnagrams",[42,213,214],{"class":56},"(",[42,216,218],{"class":217},"sjlk-","str1",[42,220,83],{"class":56},[42,222,223],{"class":217},"str2",[42,225,226],{"class":56},") {\n",[42,228,229],{"class":44,"line":60},[42,230,232],{"emptyLinePlaceholder":231},true,"\n",[42,234,235],{"class":44,"line":67},[42,236,237],{"class":63},"  \u002F\u002F Remove whitespace and lowercase\n",[42,239,240,243,246,248,251,254,256,259,262,264,267,269,272,275,278],{"class":44,"line":92},[42,241,242],{"class":48},"  let",[42,244,245],{"class":56}," s1 ",[42,247,73],{"class":48},[42,249,250],{"class":56}," str1.",[42,252,253],{"class":52},"replace",[42,255,214],{"class":56},[42,257,258],{"class":79},"\u002F",[42,260,261],{"class":129},"\\s",[42,263,258],{"class":79},[42,265,266],{"class":48},"g",[42,268,83],{"class":56},[42,270,271],{"class":79},"''",[42,273,274],{"class":56},").",[42,276,277],{"class":52},"toLowerCase",[42,279,280],{"class":56},"();\n",[42,282,283,285,288,290,293,295,297,299,301,303,305,307,309,311,313],{"class":44,"line":111},[42,284,242],{"class":48},[42,286,287],{"class":56}," s2 ",[42,289,73],{"class":48},[42,291,292],{"class":56}," str2.",[42,294,253],{"class":52},[42,296,214],{"class":56},[42,298,258],{"class":79},[42,300,261],{"class":129},[42,302,258],{"class":79},[42,304,266],{"class":48},[42,306,83],{"class":56},[42,308,271],{"class":79},[42,310,274],{"class":56},[42,312,277],{"class":52},[42,314,315],{"class":56},"();  \n",[42,317,318],{"class":44,"line":117},[42,319,232],{"emptyLinePlaceholder":231},[42,321,322],{"class":44,"line":123},[42,323,324],{"class":63},"  \u002F\u002F Check length\n",[42,326,327,329,332,335,338,341,343,346,349,352],{"class":44,"line":144},[42,328,126],{"class":48},[42,330,331],{"class":56}," (s1.",[42,333,334],{"class":129},"length",[42,336,337],{"class":48}," !==",[42,339,340],{"class":56}," s2.",[42,342,334],{"class":129},[42,344,345],{"class":56},") ",[42,347,348],{"class":48},"return",[42,350,351],{"class":129}," false",[42,353,354],{"class":56},";\n",[42,356,357],{"class":44,"line":153},[42,358,232],{"emptyLinePlaceholder":231},[42,360,361],{"class":44,"line":158},[42,362,363],{"class":63},"  \u002F\u002F Sort and compare\n",[42,365,366,368,371,373,376,379,381,383,385,388,391,394,396,398],{"class":44,"line":164},[42,367,242],{"class":48},[42,369,370],{"class":56}," sorted1 ",[42,372,73],{"class":48},[42,374,375],{"class":56}," s1.",[42,377,378],{"class":52},"split",[42,380,214],{"class":56},[42,382,271],{"class":79},[42,384,274],{"class":56},[42,386,387],{"class":52},"sort",[42,389,390],{"class":56},"().",[42,392,393],{"class":52},"join",[42,395,214],{"class":56},[42,397,271],{"class":79},[42,399,400],{"class":56},"); \n",[42,402,404,406,409,411,413,415,417,419,421,423,425,427,429,431],{"class":44,"line":403},12,[42,405,242],{"class":48},[42,407,408],{"class":56}," sorted2 ",[42,410,73],{"class":48},[42,412,340],{"class":56},[42,414,378],{"class":52},[42,416,214],{"class":56},[42,418,271],{"class":79},[42,420,274],{"class":56},[42,422,387],{"class":52},[42,424,390],{"class":56},[42,426,393],{"class":52},[42,428,214],{"class":56},[42,430,271],{"class":79},[42,432,433],{"class":56},");\n",[42,435,437],{"class":44,"line":436},13,[42,438,232],{"emptyLinePlaceholder":231},[42,440,442,444,446,449],{"class":44,"line":441},14,[42,443,167],{"class":48},[42,445,370],{"class":56},[42,447,448],{"class":48},"===",[42,450,451],{"class":56}," sorted2;\n",[42,453,455],{"class":44,"line":454},15,[42,456,232],{"emptyLinePlaceholder":231},[42,458,460],{"class":44,"line":459},16,[42,461,462],{"class":56},"}\n",[20,464,465],{},"The process is essentially the same - clean up the strings, check lengths, sort and compare.",[20,467,468],{},"This demonstrates how the core algorithm remains the same across languages, with minor syntactical differences.",[20,470,471],{},"Checking if two strings are anagrams is a common interview challenge, and solving it shows you can work with strings and sorting algorithms. With practice, translating a solution between Python and JavaScript gets easier.",[20,473,474],{},"Let me know if you would like me to expand or modify this draft blog post in any way!",[476,477,478],"style",{},"html pre.shiki code .sDgYj, html code.shiki .sDgYj{--shiki-default:#D73A49;--shiki-dark:#FF7B72}html pre.shiki code .sVjZL, html code.shiki .sVjZL{--shiki-default:#6F42C1;--shiki-dark:#D2A8FF}html pre.shiki code .szmH1, html code.shiki .szmH1{--shiki-default:#24292E;--shiki-dark:#E6EDF3}html pre.shiki code .sPyeA, html code.shiki .sPyeA{--shiki-default:#6A737D;--shiki-dark:#8B949E}html pre.shiki code .sNbzA, html code.shiki .sNbzA{--shiki-default:#032F62;--shiki-dark:#A5D6FF}html pre.shiki code .sPgVT, html code.shiki .sPgVT{--shiki-default:#005CC5;--shiki-dark:#79C0FF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sjlk-, html code.shiki .sjlk-{--shiki-default:#E36209;--shiki-dark:#FFA657}",{"title":37,"searchDepth":67,"depth":67,"links":480},[481,482],{"id":26,"depth":60,"text":27},{"id":192,"depth":60,"text":193},"2023-01-14","md",{"type":486,"layout":487},"post","blog_page","\u002Fblog\u002F2023-01-14-anagram",{"title":12,"description":22},"blog\u002F2023-01-14-anagram",[36,492,493],"programming","interview","2_B_S6F72xODmlaZR0GQSQPw1MxR3Y-cat9lQafSg7w",[496,501],{"title":497,"path":498,"stem":499,"date":500,"children":-1},"Running Traefik in Docker","\u002Fblog\u002F2022-12-09-running-traefik","blog\u002F2022-12-09-running-traefik","2022-12-09",{"title":502,"path":503,"stem":504,"date":505,"children":-1},"Leveraging Map, Reduce and Filter for Cleaner Code","\u002Fblog\u002F2023-07-21-map-reduce-filter","blog\u002F2023-07-21-map-reduce-filter","2023-07-21",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":507},"\u003Cpath fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"m12 19l-7-7l7-7m7 7H5\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":509},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Cpath d=\"M11.017 2.814a1 1 0 0 1 1.966 0l1.051 5.558a2 2 0 0 0 1.594 1.594l5.558 1.051a1 1 0 0 1 0 1.966l-5.558 1.051a2 2 0 0 0-1.594 1.594l-1.051 5.558a1 1 0 0 1-1.966 0l-1.051-5.558a2 2 0 0 0-1.594-1.594l-5.558-1.051a1 1 0 0 1 0-1.966l5.558-1.051a2 2 0 0 0 1.594-1.594zM20 2v4m2-2h-4\"\u002F>\u003Ccircle cx=\"4\" cy=\"20\" r=\"2\"\u002F>\u003C\u002Fg>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":511},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Cpath d=\"M19 21v-2a4 4 0 0 0-4-4H9a4 4 0 0 0-4 4v2\"\u002F>\u003Ccircle cx=\"12\" cy=\"7\" r=\"4\"\u002F>\u003C\u002Fg>",1783616166042]