Final test case failing Please help me understand the mistake


  • 0
    J

    import java.util.LinkedHashMap;
    import java.util.Map;

    class Solution{
    boolean isAnagram(String x,String y){
    if(x.length()!=y.length())
    return false;
    if(x == "" || y=="")
    return false;
    x=x.toLowerCase();
    y=y.toLowerCase();
    Map<Character,Integer> m1 = new LinkedHashMap<Character,Integer>();
    for(char ch ='a';ch<='z';ch++)
    {
    m1.put(ch,0);

    }
    Map<Character,Integer> m2 = new LinkedHashMap<Character,Integer>();
    for(char ch ='a';ch<='z';ch++)
    {
    m2.put(ch,0);
    }
    for(int i=0;i<x.length();i++)
    {
    m1.put(x.charAt(i),m1.get(x.charAt(i))+1);
    }

    for(int j=0;j<y.length();j++)
    {
    m2.put(y.charAt(j),m2.get(y.charAt(j))+1);
    }

    for(char c ='a';c<='z';c++)
    {

    if(m1.get(c)!=m2.get(c))
    {
    return false;
    }
    

    }

    return true;

    }}


Log in to reply
 

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