fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. System.out.println(isAnagram("anagram","nagaram"));
  14. }
  15. public static boolean isAnagram(String s, String t) {
  16. HashMap<Character,Integer> map = new HashMap<>();
  17. if(s.length() != t.length()) return false;
  18. for(int i=0;i<s.length();i++){
  19. map.put(s.charAt(i), map.getOrDefault(s.charAt(i),0)+1);
  20. }
  21. for(int j=0;j<t.length();j++){
  22. if(!map.containsKey(t.charAt(j))) return false;
  23. map.put(t.charAt(j), map.get(t.charAt(j))-1);
  24. if(map.get(t.charAt(j)) == 0) map.remove(t.charAt(j));
  25. }
  26. return true;
  27.  
  28. }
  29. }
Success #stdin #stdout 0.08s 52608KB
stdin
Standard input is empty
stdout
true