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. String s = "anagram";
  14. String t = "nagaraa";
  15. System.out.println(isAnagram(s,t));
  16. }
  17. public static boolean isAnagram(String s, String t) {
  18. if (s.length() != t.length()) {
  19. return false;
  20. }
  21.  
  22. Map<Character, Integer> counter = new HashMap<>();
  23.  
  24. for (int i = 0; i < s.length(); i++) {
  25. char ch = s.charAt(i);
  26. counter.put(ch, counter.getOrDefault(ch, 0) + 1);
  27. }
  28.  
  29. for (int i = 0; i < t.length(); i++) {
  30. char ch = t.charAt(i);
  31. if (!counter.containsKey(ch) || counter.get(ch) == 0) {
  32. return false;
  33. }
  34. counter.put(ch, counter.get(ch) - 1);
  35. }
  36.  
  37. return true;
  38. }
  39. }
Success #stdin #stdout 0.11s 54708KB
stdin
Standard input is empty
stdout
false