Help me in solving GRANAMA problem

My issue

Can anyone help me in solving this problem?

My code

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		for(int i = 0 ;  i<n ; i++)
		{
		    String k = sc.next();
		    String k1 = sc.next();
		    ArrayList<Character>a1 = new ArrayList<>();
		    HashSet<Character>h1 = new HashSet<>();
		    for(int j = 0 ; j<k.length() ; j++)
		    {
		        char ch = k.charAt(j);
		        h1.add(ch);
		        a1.add(ch);
		    }
		    ArrayList<Character>a2 = new ArrayList<>();
		    HashSet<Character>h2 = new HashSet<>();
		    for(int k2 = 0 ; k2<k1.length() ; k2++)
		    {
		        char ch1 = k1.charAt(k2);
		        h2.add(ch1);
		        a2.add(ch1);
		    }
		    if((h1.size()==h2.size()) && (a1.size()!=a2.size()))
		    {
		        System.out.println("NO");
		    }
		    else
		    {
		        System.out.println("YES");
		    }
		}
	}
}

Problem Link: Granama Recipes Practice Coding Problem - CodeChef

@catalyst_09
can be solved using hashmap
plzz refer my c++ code for better understanding .
ping me in case u get stuck at any point

#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--)
	{
	    string a,b;
	    cin>>a>>b;
	    map<char,int> mp,mp1;
	    for(int i=0;i<a.size();i++)
	    {
	        mp[a[i]]++;
	    }
	    for(int i=0;i<b.size();i++)
	    {
	        mp1[b[i]]++;
	    }
	    int fg=0;
	    if(mp.size()==mp1.size())
	    {
	        for(auto x:mp)
	        {
	            if(!mp1.count(x.first))
	            {
	                fg=0;
	                break;
	            }
	            else if(mp1[x.first]!=x.second)
	            {
	                fg=1;
	            }
	        }
	        if(fg==1)
	        cout<<"NO";
	        else
	        cout<<"YES";
	    }
	    else
	    cout<<"YES";
	    cout<<endl;
	}
	return 0;
}

Thanks brother for your help, but I actually solved this problem later on my own. Its just at the last moment I got stuck. But Thank you for your generous time. <3

1 Like