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 :: iframe reload src 
Javascript :: poo js 
Javascript :: window.scrollto(0 0) not working 
Javascript :: create angular project 
Javascript :: html set textarea value 
Javascript :: node javascript foreach limit 
Javascript :: ajax get method in jquery 
Javascript :: regex remove spaces 
Javascript :: nodejs emit event from class 
Javascript :: jquery set span text by id 
Javascript :: toastr 
Javascript :: print random string from an array to screen in javascript 
Javascript :: reload data in datatable 
Javascript :: array filter 
Javascript :: router.push in vue 3 
Javascript :: random letter from a name js 
Javascript :: how to check if checkbox is checked in jquery 
Javascript :: nginx react router 
Javascript :: timepicker in jquery 
Javascript :: how remove the spaces from the string, then return the resultant string 
Javascript :: jquery body remove class 
Javascript :: async import javascript 
Javascript :: preload 
Javascript :: import js in jupyter notebook 
Javascript :: javascript remove array element 
Javascript :: mongodb mongoose update an element in an array of objects 
Javascript :: how to filter list of objects by an array in javascript 
Javascript :: angularjs find and update object in array 
Javascript :: hide a div in jquery 
Javascript :: npm is not recognized 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =