DEV Community

talent
talent

Posted on

3 3

#3.Basic algorithms problems for frontend developers.

Check if a string is the anagram of another string.

Easy example:abba is the anagram of baab because it has the same number of characters and the same letters.

const anagrams = (string1, string2) => {
  /* call a cleanString function to clean up both 
  string of white spaces or other characaters such as ! or ? */
  const str1 = cleanString(string1);
  const str2 = cleanString(string2);

  /* if the length of string one is diffrent 
  than the length of string 2, we don't have an anagram */
  if (str1.length !== str2.length) {
    return false;
  }

  /* make both string an array, 
  sort it then join it again in a string. 
  If both string are the same we have an anagram */
  return str2.split('').sort().join('') === str1.split('').sort().join('');
};

/* helper function to clean a string 
of empty spaces or special characters */
const cleanString = str => {
  return str.replace(/[^\w]/g, '').toLowerCase();
};

console.log(anagrams('adrian ?', 'nairda !!'));
Enter fullscreen mode Exit fullscreen mode

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free โ†’

๐Ÿ‘‹ Kindness is contagious

Please leave a โค๏ธ or a friendly comment on this post if you found it helpful!

Okay