Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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

detect two strings are anagram of each other in JavaScript

function checkAnagram(str1, str2)
{
    let s1 = str1.trim().split(" ").join("").split("").sort();
    let s2 = str2.trim().split(" ").join("").split("").sort();
    if(s1.length!=s2.length) console.log("False");
    else{
        result = true;
        for(let i=0; i<s1.length; i++){
            if(s1[i]!=s2[i]){
                result = false;
                break;
            }
        }
        if(result) console.log("True");
        else console.log("False");
    }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: apa itu this pada javascript 
Javascript :: js array sort 
Javascript :: js enter key event listener 
Javascript :: input radio checked jquery 
Javascript :: js alphabets array 
Javascript :: last element of an array javascript 
Javascript :: jquery this 
Javascript :: parse int into 2 digits format javascript 
Javascript :: javascript replace last character 
Javascript :: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory 
Javascript :: sort a dictionary by value in javascript 
Javascript :: require("history").createBrowserHistory` instead of `require("history/createBrowserHistory")` 
Javascript :: moment use in angular 
Javascript :: statusbar.sethidden(true) in react native 
Javascript :: javascript execute string code 
Javascript :: get date javascript format 
Javascript :: how to wait until a variable is set javascript 
Javascript :: map add key to object in array javascript 
Javascript :: react-select dropdown open inside modal 
Javascript :: check if file is empty javascript fs 
Javascript :: react image compression 
Javascript :: refresh page javascript 
Javascript :: how to update a json file javascript 
Javascript :: javascript deep clone 
Javascript :: device width js 
Javascript :: splidejs autoscroll pauseOnHover 
Javascript :: Add event listener for loop 
Javascript :: You need to authorize this machine using `npm adduser` 
Javascript :: react native socket io 
Javascript :: first duplicate javascript 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =