Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for JavaScript LeetCode Valid Anagram
Diego (Relatable Code)
Diego (Relatable Code)

Posted on • Originally published atrelatablecode.com on

     

JavaScript LeetCode Valid Anagram

Prompt

AnAnagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

Example 1:

Input: s = "anagram", t = "nagaram" Output: true
Enter fullscreen modeExit fullscreen mode

Analyzing the problem

Let’s this of what the problem is asking. We have to know the contents of the strings we’re analyzing. We also have to make sure that we’re keeping track of the letters as they should be exactly the same to confirm that it is ananagram.

Following that train of thought, we can early return if there is ever a difference in the letters they contain. But what does it mean to have the same letters? Well, essentially it’s mapping out the frequency of each letter for the first word we iterate over. When we iterate over the second word we can double-check with what.

Checking back on the first example provided we can visualize this by counting each letter:

javascript leetcode

Looking at the counts we can use this to validate in place as we iterate over the second word. Let’s see how this would look:

javascript leetcode

So the first time we run into the n in the second iteration we subtract it from the count. Anytime we go below 0 then we know the words arenot valid anagrams.

Cool 😎, let’s get to coding.

The solution

Similar to ourlast problem we can use a hash table.

varisAnagram=function(s,t){if(s.length!=t.length)returnfalse;consthashTable={};for(leti=0;i<s.length;i++){if(!hashTable[s[i]]){hashTable[s[i]]=0;}hashTable[s[i]]++;}for(letj=0;j<t.length;j++){if(!hashTable[t[j]]){returnfalse;}hashTable[s[j]]--;}returntrue;};
Enter fullscreen modeExit fullscreen mode

In the first iteration, we add the frequency of every letter. If it doesn’t exist we create a 0 value at that point.

if(!hashTable[s[i]]){hashTable[s[i]]=0;}hashTable[s[i]]++;
Enter fullscreen modeExit fullscreen mode

On the second iteration, we subtract all values. If no letter ever exists then we’ll hit the number 0 which means we'll return a false.

if(!hashTable[t[j]]){returnfalse;}hashTable[s[j]]--;
Enter fullscreen modeExit fullscreen mode

Let’s connect

If you liked this feel free to connect with me onLinkedIn orTwitter

Check out my free developer roadmap and weekly tech industry news in mynewsletter.

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
jonrandy profile image
Jon Randy 🎖️
🤖 Artisanal developer - coding with varying degrees of success since 1983
  • Location
    Bangkok 🇹🇭
  • Joined
• Edited on• Edited
constisAnagram=(s,t)=>''+[...s].sort()==[...t].sort()
Enter fullscreen modeExit fullscreen mode
CollapseExpand
 
diballesteros profile image
Diego (Relatable Code)
🚀 JS/React/Typescript Developer 🛠️ Self-taught developerNew articles every MondayLet's connect on Twitter https://twitter.com/relatablecoder
  • Location
    Bogota D.C., Colombia
  • Work
    Senior Developer
  • Joined

Nice, I like the one liner!

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

🚀 JS/React/Typescript Developer 🛠️ Self-taught developerNew articles every MondayLet's connect on Twitter https://twitter.com/relatablecoder
  • Location
    Bogota D.C., Colombia
  • Work
    Senior Developer
  • Joined

More fromDiego (Relatable Code)

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp