Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

anagram

//find the anagram if two word has the same characters for example word 1: listen, word 2: silent, if the characters are the same return true otherwise return false

//str1="eat"  str2="ate"
//str1="bar " str2="bare"
//str1="bba"  str2="aab"
function areAnagram(str1,str2)
{
    // Get lengths of both strings
    let n1 = str1.length;
    let n2 = str2.length;

    // If length of both strings is not same,
    // then they cannot be anagram
    if (n1 != n2)
        return false;

    // Sort both strings
    let x =str1.split("").sort();
    let y  =str2.split("").sort()
    console.log(x ,y);

    // Compare sorted strings
    for (let i = 0; i < n1; i++){

        if (x[i] != y[i])
            return false;
    }

    return true;
}
let str1="netsil";
let str2="listen";

console.log( areAnagram(str1,str2));
Source by www.javatpoint.com #
 
PREVIOUS NEXT
Tagged: #anagram
ADD COMMENT
Topic
Name
7+1 =