Is Anagram

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.

1 Like
#include<iostream>
#include<string.h>
using namespace std;
void sort(char str[]);
int main()
{
    int len1,len2;
    char a[100],b[100];
    cin>>a;
    len1=strlen(a);
    sort(a);
    cin>>b;
    len2=strlen(b);
    sort(b);
    if(len1==len2){
        if(strcmp(b,a)==0){
            cout<<"Yes";
        }
        else{
            cout<<"No";
        }
    }
    return 0;
}
void sort(char str[])
{
    int i,j,len;
    char temp;
    for(len=0;str[len];len++);
    for(i=1;i<len;i++){
        for(j=0;j<len-i;j++){
            if(str[j]>str[j+1]){
                temp=str[j];
                str[j]=str[j+1];
                str[j+1]=temp;
            }
        }
    }
}

What is the problem in this program???

string s,w;
cin>>s>>w;
sort(s.begin(),s.end());
sort(w.begin(),w.end());

s==w?cout<<"YES":cout<<"NO";

What’s wrong with this algorithm??
Anybody??

I got stuck at 7th case, what is the problem in my code

Python 3.7

a = input()
b = input()
c = []
d = 'No'

for i in range(len(a)):
    c.append(a[i])
    if (b[i] in c) and len(a)== len(b):
        d = 'Yes'
    else:
        d = 'no'
print(d)

You misspelled yes and no.
You were supposed to print “Yes” or “No” instead of “YES” or “NO”.

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

1 Like
#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?

i can’t understanding to here . how can comparing two string each other somebody tell how can it work
if(a[j]==b[i]){
b[i]=0;
n++;

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

What problem last case worng answer

Can anyone tell me why am i getting wrong answer?

import java.util.Arrays;
import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner user_input = new Scanner(System.in);
        String s1 = user_input.next();
        String s2 = user_input.next();
        char[] sorter = s1.toCharArray();
        Arrays.sort(sorter);
        s1 = new String(sorter);
        sorter = s2.toCharArray();
        Arrays.sort(sorter);
        s2 = new String(sorter);
        if (s1.equals(s2))
            System.out.println("Yes");
        else 
            System.out.println("NO");
    }
}

But using this code, I get wrong answer in the test case 7. Can you help me???

a = str(input())
b = str(input())
for i in a:
        for j in b:
                if i == j:
                        print("Yes")
                        break
        else:
                print("No")
                break
        break

@murad928 What if a=“an” and b=“aan”

1 Like

def check(A, Q):

	if(sorted(A)== sorted(Q)):
	print("Yes")
else:
	print("No")		

A= “listen”
Q= “silent”
check(Q, A)

what’s wrong with this code?

Why wrong anser in 7 case??

#include <stdio.h>
#include <string.h>
int main() {
	char A[20],B[20];
	int i,j,t,length;
	scanf("%s%s",&A,&B);
	length = strlen(A);

	if(strlen(A) != strlen(B)){
		t = 0;
	}
	else{
		for(i = 0; i < length; i++){
			for(j = 0; j <= i ; j++){
				if(A[i] == B[j]){
					t = 1;
				}
			}
	}
	}
	if(t){
		printf("Yes");
	}
	else{
		printf("No");
	}
	return 0;
}