import java.util.HashMap;
public class Main {
public static void main(String[] args) {
//Use inputs from https://pastebin.com/DMQ6xqKe and assign to s * t, as stackoverflow limited character use
String s = "";
String t = "";
System.out.println(isAnagram(s, t));
}
public static boolean isAnagram(String s, String t) {
HashMap<Character, Integer> sMap = new HashMap<>();
HashMap<Character, Integer> tMap = new HashMap<>();
if(s.length() != t.length())
{
return false;
}
for(int i = 0; i < s.length(); i++)
{
if(sMap.containsKey(s.charAt(i)))
{
sMap.replace(s.charAt(i), sMap.get(s.charAt(i)) + 1);
}
else
{
sMap.put(s.charAt(i), 1);
}
}
for(int i = 0; i < t.length(); i++)
{
if(tMap.containsKey(t.charAt(i)))
{
tMap.replace(t.charAt(i), tMap.get(t.charAt(i)) + 1);
}
else
{
tMap.put(t.charAt(i), 1);
}
}
//FAILS BUT WHY!?!??!?
for(int i = 0; i < s.length(); i++)
{
System.out.println("outside " + sMap.get(s.charAt(i)) + " compared to " + tMap.get(s.charAt(i)));
if(sMap.get(s.charAt(i)) != tMap.get(s.charAt(i)))
{
System.out.println(sMap.get(s.charAt(i)) + " compared to " + tMap.get(s.charAt(i)));
return false;
}
}
//PASSES BUT PREVIOUS FAILS?!!?!??!
// if(!sMap.equals(tMap))
// {
// return false;
// }
return true;
}
}
This code fails but the commented code using .equals works. I don't really understand. Logged the values, they matched but the check fails saying different values.