Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

anagram program in javascript

function checkStringsAnagram(a, b) {
   let len1 = a.length;
   let len2 = b.length;
   if(len1 !== len2){
      console.log('Invalid Input');
      return
   }
   let str1 = a.split('').sort().join('');
   let str2 = b.split('').sort().join('');
   if(str1 === str2){
      console.log("True");
   } else { 
      console.log("False");
   }
}
checkStringsAnagram("indian","ndiani")
Comment

anagram js

const isAnagram = (str1, str2) => {
	const str2Arr = str2.split('');
	for(let i = 0; i < str1.length; i++) {
		const index = str2Arr.indexOf(str1[i]);
		if(index === -1) return false;
   		str2Arr.splice(index, 1);
    }
	return !str2Arr.length;
}
Comment

javascript anagram

const s = "anagram"
const t = "nagaram"

const isAnagram = function(s, t) {
    if (s.length !== t.length) {
        return false
    }

    const map_s = new Map()
    const map_t = new Map()

    for (let i = 0; i < s.length; i++) {
        if (map_s.has(s[i])) {
            map_s.set(s[i], map_s.get(s[i]) + 1)
        } else {
            map_s.set(s[i], 1)
        }

        if (map_t.has(t[i])) {
            map_t.set(t[i], map_t.get(t[i]) + 1)
        } else {
            map_t.set(t[i], 1)
        }
    }

    for (const s of map_s) {
        if (map_t.get(s[0]) !== s[1]) {
            return false
        }
    }

    console.log(map_s)
    console.log(map_t)
  	// [Log]:
    // Map(5) { 'a' => 3, 'n' => 1, 'g' => 1, 'r' => 1, 'm' => 1 }
    // Map(5) { 'n' => 1, 'a' => 3, 'g' => 1, 'r' => 1, 'm' => 1 }

    return true
};

console.log('
', isAnagram(s, t)) 	  // [Log]: true
Comment

anagram javascript example

function anagram(name, words) {
	var a = name.replace(/s/g,'').toLowerCase().split('').sort().join('');
	var b = words.join('').split('').sort().join('');
	return a == b;
}
Comment

anagram program in javascript

    function group_anagrams(arr) {
      let   sortedArr = arr.map(item => item.split('').sort().join(''));
      let setArr = new Set(sortedArr);
      let reducedObj = {};
      for (let setItem of setArr) {
        let indexArr = sortedArr.reduce((acc, cur, index) => {
          if (setItem === cur) {
            acc.push(index);
          }
          return acc;
        }, []);
        reducedObj[setItem] = indexArr;
      }
      let finalArr = [];
      for (let reduceItem in reducedObj) {
        finalArr.push(reducedObj[reduceItem].map(item => arr[item]));
      }
      return finalArr;
    }
    group_anagrams(['car','cra','rca', 'cheese','ab','ba']);
Comment

anagram js

function isAnagram(stringA, stringB) {
  // Sanitizing
  stringA = stringA.toLowerCase().replace(/[W_]+/g, "");
  stringB = stringB.toLowerCase().replace(/[W_]+/g, "");

  // sorting
  const stringASorted = stringA.split("").sort().join("");
  const stringBSorted = stringB.split("").sort().join("");

  return stringASorted === stringBSorted;
}
Comment

anagram javascript example

function anagram(name, words) {
	name =  name.toLowerCase().split('').sort().join('').trim();
  words = words.join('').toLowerCase().split('').sort().join('');
  return name === words;
}
Comment

anagram javascript

const isAnagram = (str1, str2) => {
	const str2Arr = str2.split('');
	for(let i = 0; i < str1.length; i++) {
		const index = str2Arr.indexOf(str1[i]);
		if(index === -1) return false;
   		str2Arr.splice(index, 1);
    }
	return !str2Arr.length;
}
console.log(isAnagram("abba","bbaa"))
Comment

PREVIOUS NEXT
Code Example
Javascript :: connect mysql to node js 
Javascript :: js convert string array to number array 
Javascript :: network display react native 
Javascript :: js how to filter only real numbers from decimals 
Javascript :: javascript date object format yyyy mm dd 
Javascript :: set 404 handling via express in node 
Javascript :: simple alert program in javascript 
Javascript :: javascript loop x times 
Javascript :: char array to string javascript 
Javascript :: javascript get focusable elements 
Javascript :: reducer in react example 
Javascript :: async await javascript stack overflow 
Javascript :: equivalent method load jquery with javascript 
Javascript :: sqrt javascript 
Javascript :: js check if undefined 
Javascript :: bind an event to dom element angular 
Javascript :: repeat array in js 
Javascript :: how to send axios delete to the backend reactjs 
Javascript :: react Refused to execute inline script because it violates the following Content Security Policy directive 
Javascript :: get query params react 
Javascript :: parse string to int nodejs 
Javascript :: javascript get nested element 
Javascript :: swap function javascript 
Javascript :: custom react native product rating 
Javascript :: reverse array javascript 
Javascript :: map list in javascript 
Javascript :: javascript increment by 1 
Javascript :: cm to feet javascript 
Javascript :: axios get 
Javascript :: set background image URL jQuery 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =