Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- public static void main(String[] args) {
- System.out.println(anagram("abefgchd", "abcdefgh"));
- }
- // Write a method to decide if two strings are anagrams or not.
- public static boolean anagram(String str1, String str2) {
- if (str1.length() != str2.length()) {
- return false;
- }
- int[] abc = new int[256];
- for (int i = 0; i < str1.length(); i++) {
- int index = str1.charAt(i);
- abc[index]++;
- }
- for (int i = 0; i < str2.length(); i++) {
- int index = str2.charAt(i);
- abc[index]--;
- if (abc[index] < 0) {
- return false;
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement