Compare commits

...

2 Commits

Author SHA1 Message Date
Akshat
b900ab5f0a
Merge ca3103f1ba into 2c67b48c21 2024-04-25 08:17:37 +08:00
AkshatAggEm
ca3103f1ba added frequency counter algorithm based problems. 2019-10-05 17:38:27 +05:30
4 changed files with 139 additions and 0 deletions

View 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");

View 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]);

View File

@ -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);

View 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);