Advertisement
zoltanvi

Anagram checker

Jul 16th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.75 KB | None | 0 0
  1. public class Main {
  2.  
  3.     public static void main(String[] args) {
  4.         System.out.println(anagram("abefgchd", "abcdefgh"));
  5.     }
  6.  
  7.     // Write a method to decide if two strings are anagrams or not.
  8.     public static boolean anagram(String str1, String str2) {
  9.         if (str1.length() != str2.length()) {
  10.             return false;
  11.         }
  12.         int[] abc = new int[256];
  13.  
  14.         for (int i = 0; i < str1.length(); i++) {
  15.             int index = str1.charAt(i);
  16.             abc[index]++;
  17.         }
  18.  
  19.         for (int i = 0; i < str2.length(); i++) {
  20.             int index = str2.charAt(i);
  21.             abc[index]--;
  22.             if (abc[index] < 0) {
  23.                 return false;
  24.             }
  25.         }
  26.  
  27.         return true;
  28.     }
  29.  
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement