Java String Anagram Solver anagrams(String a, String b)

Here you can find the source of anagrams(String a, String b)

Description

anagrams

License

Apache License

Declaration

public static boolean anagrams(String a, String b) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.Arrays;

public class Main {
    public static boolean anagrams(String a, String b) {
        if (a == null && b == null) {
            return true;
        }/*from   w w  w .j av a  2 s .c  om*/
        if (a == null || b == null) {
            return false;
        }
        if (a.length() != b.length()) {
            return false;
        }

        char[] charsa = a.toCharArray();
        char[] charsb = b.toCharArray();

        Arrays.sort(charsa);
        Arrays.sort(charsb);

        if ((String.valueOf(charsa)).equals(String.valueOf(charsb))) {
            return true;
        }
        return false;
    }
}