# Whether two strings is an anagram?

• ``````bool isAnagram(const string& s1, const string& s2)
{
if (s1.length() != s2.length())
return false;
int charMap[256] = { 0 };
for (int i = 0; i < s1.length(); ++i)
++charMap[(int)s1[i]];
for (int i = 0; i < s2.length(); ++i)
if (--charMap[(int)s2[i]] < 0)
return false;
return true;
}    // O(n)``````

• #include<stdio.h>
#include<string.h>

//int *bool( *ptr1 , *ptr2)
//char *ptr1 , *ptr2;

int main ()
{

char str1[100];

char str2[100];

char *p1 , *p2;

``````printf(" \n enter the first string");

gets(str1);

printf (" \n  enter the second string ");

gets(str2);

p1 = str1;

p2 = str2;

int count1 =0 ,count2 = 0,l1,l2;

l1 = strlen(str1);

l2 = strlen(str2);

printf(" \n len1=%d, len2=%d", l1,l2);

while (*p1 != '\0'){
count1++;
p1++;
}
``````

while (*p2 != '\0')
{
count2++;
p2++;
}
printf(" \n len1=%d, len2=%d", l1,l2);
printf(" \n the lenth of the string are :: %d %d", count1,count2);

``````if(count1 == count2)
printf(" \n ok now anagram can be found");
else
printf(" \n sorry");
int i, j;

if (l1 == l2){
for (i=0 ; i<l1 ;i++){
for(j=0 ; j<l1 ; j++){
if (str1[ j] == str2[i]);
}
}

printf ("\n yes it is a anagram");
}

else ("sorry");
``````

return 0;
}

• ``````# is anagram or not
import os
import sys
import string

class Anagram:
def isAnagram(self, str1, str2):
self.str1 = str1
self.str2 = str2

if len(str1) != len(str2):
return False

# Initialize the list of characters to 0 initially and number of alphabets are constant =26
listOfChars = [0] * 26
``````

#this will create the list of incrementing 1 for each character found ...and if there were for example 3 c's in #str1 then index for listOfChars will have value of 3

``````		for i in range(0, len(str1)):
index = ord(str1[i]) - ord('a')
listOfChars[index] += 1

for i in range(0, len(str2)):
index = ord(str2[i]) - ord('a')
listOfChars[index] -= 1

for i in range(0, len(listOfChars)):
if (listOfChars[i]) != 0:
return False

return True

myAnagram = Anagram()
print(myAnagram.isAnagram('tale', 'late'))
``````

• This is a way to do it in O(n) time but it violates Google's JavaScript styleguide: https://google.github.io/styleguide/javascriptguide.xml#delete

``````/**
@param {string} inputA
@param {string} inputB
@return {boolean}
*/
var isAnagram = function(inputA, inputB) {
if (inputA.length > inputB.length) { return false; }
var characterCounts = {}
for (var i = 0; i < inputA.length; i++) {
var characterA = inputA.charAt(i);
var characterB = inputB.charAt(i);
if (!characterCounts[characterA]) {
characterCounts[characterA] = 1;
} else {
characterCounts[characterA]++
}
if (!characterCounts[characterB]) {
characterCounts[characterB] = -1;
} else {
characterCounts[characterB]--
}
if (characterCounts[characterA] === 0) {
delete characterCounts[characterA]
}
if (characterCounts[characterB] === 0) {
delete characterCounts[characterB]
}
}
for (var character in characterCounts) {
if (Object.hasOwnProperty.call(characterCounts, character)) {
return false;
}
}
return true;
}``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.