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
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
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."
    );
}

Javascript相关代码片段

React Modal using Dialog

how to create dynamic object in javascript

how to create global variable in postman

javascript interview questions tricky

how to create workflow request in postman

mysql json_extract array

parse response body in postman

query param generator js

jest function async API

React portal

i18next suppoeort react

puppeteer page evaluate

react router dom use navigate

react create array from another array

how to delete global variable in scripts postman

Where does closure variables stored in javascript

Where does local variables stored in javascript

nodejs là gì

addEventListener for multiple selector

js calculate opacity

nodejs create stripe payment from server easy

get child form value in parent viewchild

javascript capitalize every word in a sentence

how to use jquery in moodle

how to handle no internet error in react query

window in not defined in nextjs

cypress find element inside element

app tracking transparency react native permissions

C# Blazor list to json

puppeteer .cache folder not found

css in js

bootstrap next js

creat element js

angular can't bind to ngmodel

sandy liang

Toast Message always appears behind the modal

selenium and javascript

react native textinput should not autofill

form.getfieldvalue does not change antd

javascript zoom to bumber

javascript reduce exercises

array in js mdn

javascript built in priority queue

prevent parent click events from firing

what is intrinsic function in javascript

next js with pnpm

shopify emacs

dynamic scroll longitude antd table

tree vue js

eslint no-undef console

Count documents by field mongo

vertical column checkbox.group antd

dynamically adding class to anGULAR ELEMENT

random integer between 5 and 10 javascript

javascript refresh image src

url change tag javascript

change style using getelementsbyclassname

js uppercase türkçe

javascript sorting array using for loops

next js router not mounted

show image progress download in js

`NextRouter` was not mounted

Custom Dialog for GSheet

for each js on string

calculator in js

jsonserver

sweetalerts radio alert

how to add label to kubernetes node

react observer api

javascript metodlar

fixed height react quill editor

import js to postman

extract the domain name from a url javascript

react quill font colr doesn't change

Convert String to Date in JavaScript

signalr react

js regex srong password

password strength regex

import json into a var in typescript

Merge sort tree when two array is sorted