Skip to content

Commit 8cc7357

Browse files
authored
Update js codes.docx
1 parent 819711e commit 8cc7357

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

js codes.docx

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3216,6 +3216,47 @@ var frequencySort = function(nums) {
32163216
};
32173217
console.log(frequencySort([1,1,2,2,2,3]))
32183218
==========================================================================================================================================================================
3219+
Anagram:O(N)
3220+
let NO_OF_CHARS = 256;
3221+
function areAnagram(str1, str2)
3222+
{
3223+
// Create a count array and initialize all values as 0
3224+
let count = new Array(NO_OF_CHARS).fill(0);
3225+
let i;
3226+
3227+
if (str1.length != str2.length) return false;
3228+
for(i = 0; i < str1.length; i++)
3229+
{
3230+
count[str1[i].charCodeAt(0)]++;
3231+
count[str2[i].charCodeAt(0)]--;
3232+
}
3233+
3234+
// See if there is any non-zero value in count array
3235+
for(i = 0; i < NO_OF_CHARS; i++)
3236+
if (count[i] != 0)
3237+
{
3238+
return false;
3239+
}
3240+
return true;
3241+
}
3242+
3243+
// Driver code
3244+
let str1 =
3245+
"priya".split("");
3246+
let str2 =
3247+
"riyap".split("");
3248+
3249+
// Function call
3250+
if (areAnagram(str1, str2))
3251+
document.write(
3252+
"The two strings are " +
3253+
"anagram of each other");
3254+
else
3255+
document.write(
3256+
"The two strings are " +
3257+
"not anagram of each other");
3258+
3259+
========================================================================================================================================================================
32193260

32203261

32213262

@@ -3254,6 +3295,7 @@ function convertToORBasedExpression (arr){
32543295
}
32553296
convertToORBasedExpression(inputArray)
32563297

3298+
---------------------------------
32573299

32583300

32593301

0 commit comments

Comments
 (0)