Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

How to check for Anagrams in python

from collections import Counter
def is_anagram(s1, s2):
  return Counter(s1) == Counter(s2)

s1 = 'listen'
s2 = 'silent'
s3 = 'runner'
s4 = 'neuron'

print(''listen' is an anagram of 'silent' -> {}'.format(is_anagram(s1, s2)))
print(''runner' is an anagram of 'neuron' -> {}'.format(is_anagram(s3, s4)))


# Output

# 'listen' an anagram of 'silent' -> True
# 'runner' an anagram of 'neuron' -> False
Comment

check if anagram

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

if 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));
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to log message with replacing placeholders in the string in js 
Javascript :: fastselect clear select 
Javascript :: number and type operators in javascript 
Javascript :: jquery set focus on first input which is not readonly 
Javascript :: which is faster python or node.js for image saving as server 
Javascript :: invoke method inside class javascript 
Javascript :: send data to user node 
Javascript :: Coin toss with JavaScript and HTML 
Javascript :: ternary operator online 
Javascript :: react.children.toarray explained 
Javascript :: settimeout react native focus text input 
Javascript :: locale data angular 12 
Javascript :: Calling JSON REST Services with FoxPro and wwJsonServiceClient 
Javascript :: laravel tabel json nullable 
Javascript :: remove every element of array which starts with char text 
Javascript :: sus 
Javascript :: about react frame 
Javascript :: axios try catch get status code 
Javascript :: col flex antd 
Javascript :: runjs 
Javascript :: networkx explore nodes 
Javascript :: javascript verbatim string 
Javascript :: jquery 1.6 jgrid pagging ejemplo 
Javascript :: jquery random color array 
Javascript :: check if object is empty js 
Javascript :: when reload the page the route gone in react js laravel 
Javascript :: how to get query data using javascript 
Javascript :: buffer to base 64 online 
Javascript :: Fibonacci perticular position in javascript 
Javascript :: jquery image onerror not working 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =