fork download
  1. import java.util.*;
  2.  
  3. class Ideone {
  4. public static boolean isAnagram(String s, String t) {
  5. if (s.length() != t.length()) {
  6. return false;
  7. }
  8. HashMap<Character,Integer> hm = new HashMap<>();
  9. for (int i = 0; i < s.length(); i++) {
  10. hm.put(s.charAt(i), hm.getOrDefault(s.charAt(i), 0) + 1);
  11. }
  12. for (int i = 0; i < t.length(); i++) {
  13. if (hm.containsKey(t.charAt(i)) && hm.get(t.charAt(i)) > 0) {
  14. hm.put(t.charAt(i), hm.get(t.charAt(i)) - 1);
  15. } else {
  16. return false;
  17. }
  18. }
  19. return true;
  20. }
  21.  
  22. public static void main(String[] args) {
  23. Scanner sc = new Scanner(System.in);
  24. String s = sc.next();
  25. String t = sc.next();
  26. System.out.println(isAnagram(s, t));
  27. }
  28. }
Success #stdin #stdout 0.14s 56644KB
stdin
manish
shiimans
stdout
false