mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-10 11:09:43 +08:00
Compare commits
2 Commits
93df7e5495
...
b900ab5f0a
Author | SHA1 | Date | |
---|---|---|---|
|
b900ab5f0a | ||
|
ca3103f1ba |
45
src/algorithms/Fequency Counter based/Anagrams.js
Normal file
45
src/algorithms/Fequency Counter based/Anagrams.js
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
//program to check if two hard-coded strings are anagrams or not.
|
||||||
|
|
||||||
|
function isAnagram(str1, str2) {
|
||||||
|
|
||||||
|
//firstly, they need to be of the same length.
|
||||||
|
if (str1.length !== str2.length) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//frequency counters
|
||||||
|
var frequencyCounter1 = {}
|
||||||
|
, frequencyCounter2 = {};
|
||||||
|
|
||||||
|
//value of frequency counter being updated for every string character.
|
||||||
|
/*what this actually does is, it creates an object, in which the key value pairs are as follows -->
|
||||||
|
|
||||||
|
key : value :: character : frequency
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
//added, toLowerCase() method to make it case insensitive.
|
||||||
|
for (let val of str1.toLowerCase()) {
|
||||||
|
frequencyCounter1[val] = (frequencyCounter1[val] || 0) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let val of str2.toLowerCase()) {
|
||||||
|
frequencyCounter2[val] = (frequencyCounter2[val] || 0) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let key in frequencyCounter1) {
|
||||||
|
//checks if the particular key exists or not
|
||||||
|
if (!(key in frequencyCounter2)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//then checks, if for the particular keys of the two objects, the values are equal or not.
|
||||||
|
if (frequencyCounter2[key] !== frequencyCounter1[key]) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Remember Shutter Island :P
|
||||||
|
isAnagram("Rachel Solando", "Dolores Chanal");
|
40
src/algorithms/Fequency Counter based/areEqualNoOfSquares.js
Normal file
40
src/algorithms/Fequency Counter based/areEqualNoOfSquares.js
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
//Program to check if the elements in the second array, are the squares of each and every element of the first array.
|
||||||
|
function areEqualNoOfSqs(arr1, arr2) {
|
||||||
|
|
||||||
|
//firstly, they need to be of the same length.
|
||||||
|
|
||||||
|
if (arr1.length !== arr2.length) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//frequency counters
|
||||||
|
var freqCounter1 = {}
|
||||||
|
, freqCounter2 = {};
|
||||||
|
|
||||||
|
//value of frequency counter being updated for every string character.
|
||||||
|
/*what this actually does is, it creates an object, in which the key value pairs are as follows -->
|
||||||
|
|
||||||
|
key : value :: character : frequency
|
||||||
|
|
||||||
|
*/
|
||||||
|
for (let val of arr1) {
|
||||||
|
freqCounter1[val] = (freqCounter1[val] || 0) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let val of arr2) {
|
||||||
|
freqCounter2[val] = (freqCounter2[val] || 0) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let key in freqCounter1) {
|
||||||
|
if (!(key ** 2 in freqCounter2)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (freqCounter2[key ** 2] !== freqCounter1[key]) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
areEqualNoOfSqs([2, 3, 4, 1, 2, 5], [25, 4, 9, 1, 4, 16]);
|
@ -0,0 +1,17 @@
|
|||||||
|
|
||||||
|
//Program to check if duplicate elements are present inside an array.
|
||||||
|
|
||||||
|
function areThereDuplicatesFQ(){
|
||||||
|
let obj = {};
|
||||||
|
|
||||||
|
for(let val in arguments){
|
||||||
|
obj[arguments[val]] = (obj[arguments[val]]||0)+1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(let key in obj){
|
||||||
|
if(obj[key]>1) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
areThereDuplicatesFQ(1,2,5,7,8);
|
37
src/algorithms/Fequency Counter based/sameFrequency.js
Normal file
37
src/algorithms/Fequency Counter based/sameFrequency.js
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
//Numeric anagrams detector
|
||||||
|
|
||||||
|
function sameFrequency(strA, strB) {
|
||||||
|
|
||||||
|
//convert the array, into a string
|
||||||
|
var str1 = strA.toString()
|
||||||
|
, str2 = strB.toString();
|
||||||
|
|
||||||
|
if (str1.length !== str2.length) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var freqCounter1 = {}
|
||||||
|
, freqCounter2 = {};
|
||||||
|
|
||||||
|
|
||||||
|
for (let val of str1) {
|
||||||
|
freqCounter1[val] = (freqCounter1[val] || 0) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let val of str2) {
|
||||||
|
freqCounter2[val] = (freqCounter2[val] || 0) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let key in freqCounter1) {
|
||||||
|
if (!(key in freqCounter2)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (freqCounter2[key] !== freqCounter1[key]) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
sameFrequency(552181, 152581);
|
Loading…
Reference in New Issue
Block a user