If both the strings have the same number of every character they will be anagram. If the strings have same number of ‘a’ the difference of count(‘a’) will be 0. If for every character the difference is 0 the sum will be zero. And if the sum is zero the strings will be anagram.
If I did it one by one (like: x.count(‘a’), x.count(‘b’), …), it would take much time. So used ASCII code for shortcut.(chr(65) = ‘a’, chr(66) = ‘b’ …)
That won’t work bro. AAD and ABC will be anagrams according to your logic but they are not.
There are many ways the summition of differences can be 0. Anagram is a different matter.
Figure out another way to solve this Problem.
Your code will not work. You’re just checking that elements of the second string is in the first string or not.
Think about ABC and AAA
Their lengths are same. All of the elements of second string are in first string. But they aren’t anagram.
I hope you understand
#include<stdio.h>
int main()
{
char a[99],b[99];
scanf("%s %s",&a,&b);
int l1,l2;
l1 = strlen(a);
l2 = strlen(b);
int s1=sum(a);
int s2= sum(b);
int a1 = test(a);
int b1 = test(b);
if(a1 == 1 && b1 == 1)
{
int l1,l2;
l1 = strlen(a);
l2 = strlen(b);
int s1=sum(a);
int s2= sum(b);
if(l1 == l2 && s1 == s2)
{
printf("yes");
}
else
{
printf("No");
}
}
}
int sum(char a[])
{
int s =0;
for(int i=0; i<strlen(a); i++)
{
s = s+(char)a[i];
}
return s;
}
int test(char a[])
{
int x = 0;
for(int i=0; i<strlen(a); i++)
{
if((char)a[i]>=97 && (char)a[i]<=122)
{
x = 1;
}
else
{
x = 0;
break;
}
}
return x;
}
my code is accepted. i just want to know my code is efficient or not?..i have checked all the conditions . so it’s a bit lengthy . is it efficient or not?
in this problem you can sort the given strings and then thek ,if the same podition has same value for both print yes.else no…Here is my code:
/Bismillahir Rahmanir Rahim./
#include<bits/stdc++.h> #include
#include<math.h> #include
using namespace std;
int main()
{
long long int a,b,c,d,i,x,y=0,j;
char s[1000],z[1000];
cin>>s>>z;
b=strlen(s);
c=strlen(z);
sort(s,s+b);
sort(z,z+c);
for(i=0;i<b;i++)
{
if(s[i]!=z[i])
{
y=1;break;
}
}
if(y>0) cout<<“No”<<endl;
else cout<<“Yes”<<endl;
A = input()
B = input()
a = A.lower()
b = B.lower()
for i in range(len(a)):
if len(A) == len(b):
a_sorted = ‘’.join(sorted(a))
b_sorted = ‘’.join(sorted(b))
if a_sorted == b_sorted:
print(“Yes”)
break
else:
print(“No”)
break