fork download
  1.  
  2. class Main {
  3. public static boolean isAnagram(String s, String t) {
  4. if (s.length() != t.length()) return false;
  5.  
  6. int[] freq = new int[26];
  7.  
  8. for (int i = 0; i < s.length(); i++) {
  9. freq[s.charAt(i) - 'a']++;
  10. freq[t.charAt(i) - 'a']--;
  11. }
  12.  
  13. for (int count : freq) {
  14. if (count != 0) return false;
  15. }
  16.  
  17. return true;
  18. }
  19.  
  20. public static void main(String[] args) {
  21. String s = "listen";
  22. String t = "silent";
  23.  
  24. System.out.println(isAnagram(s, t) ? "Anagram" : "Not Anagram");
  25. }
  26.  
  27. }
  28.  
Success #stdin #stdout 0.1s 54652KB
stdin
Standard input is empty
stdout
Anagram