Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript anagram check

const areAnagram = (str1, str2) => str1.toLowerCase().split('').sort().join('') === str2.toLowerCase().split('').sort().join('');

// Examples
areAnagram('listen', 'silent');         // true
areAnagram('they see', 'the eyes');     // true
areAnagram('node', 'deno');             // true
Comment

How to determine if one string is anagram of another, in javascript?

/*
  An anagram can be obtained by rearranging
  the letters of another word. For instance, 
  "anagram" is an anagram of "nagaram".
  This implementation determines if one string
  is anagram of another. The two strings are
  assumed to be composed of lowercase alphabetic
  letters only.

  Time complexity: O(n)
  Space complexity: O(1)
*/
// Return true if str1 is anagram of str2
// Or false otherwise
function isAnagram(str1, str2) {
  // Two anagrams must have same length
  if (str1.length !== str2.length) {
    return false;
  }
  // Count nb of occurrences of characters
  // in str1. Only 26 countes are needed
  // since str1 has only lowercase letters
  const counter = new Array(26).fill(0);
  let currentChar1, currentChar2, currentIdx;
  const charCodeA = "a".charCodeAt(0);

  for (let idx = 0; idx < str1.length; idx++) {
    currentChar1 = str1.charAt(idx);
    currentIdx = currentChar1.charCodeAt(0) - charCodeA;
    counter[currentIdx]++;
  }
  // Decrement the previously calculated counters
  // by scanning through letters of str2.
  for (let idx = 0; idx < str2.length; idx++) {
    currentChar2 = str2.charAt(idx);
    currentIdx = currentChar2.charCodeAt(0) - charCodeA;
    console.log(currentIdx);
    counter[currentIdx]--;
    if (counter[currentIdx] < 0) {
      return false;
    }
  }
  return true;
}

console.log(isAnagram("rat", "car")); // true
Comment

check for string anagram javascript

function compare (a, b) {
    var y = a.split("").sort().join(""),
        z = b.split("").sort().join("");
    console.log(z === y
        ? a + " and " + b + " are anagrams!"
        : a + " and " + b + " are not anagrams."
    );
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: node.js copy to clipboard 
Javascript :: react native image 
Javascript :: js seconds to time 
Javascript :: next day date javascript 
Javascript :: javjquery is emptyobject 
Javascript :: vue custom events 
Javascript :: accèder data-id javascript 
Javascript :: js merge 2 form data 
Javascript :: secure random nodejs 
Javascript :: javascript for loop array 
Javascript :: check box jquery 
Javascript :: access variable from another function javascript 
Javascript :: process.argv 
Javascript :: onfocus js 
Javascript :: print js 
Javascript :: min heap javascript 
Javascript :: reverse the string in javascript 
Javascript :: delete element html javascript 
Javascript :: remove value from input jquery 
Javascript :: find unique value on array 
Javascript :: express js delete request 
Javascript :: bracket notation javascript 
Javascript :: sequelize mariadb example 
Javascript :: javascript base64 decode 
Javascript :: iterate through object javascript 
Javascript :: axios request and response intercepters 
Javascript :: How to block ctrl+shift+j using javascript 
Javascript :: select2 multi select get selected value 
Javascript :: jquery: get selected option of the drop down list 
Javascript :: array map javascript 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =