Java Anagrams

  • + 0 comments
    public class Solution {
    
        static boolean isAnagram(String a, String b) {
            if (a.length() != b.length()) return false;
            if (sortAsc(a).compareTo(sortAsc(b)) != 0) return false;
            return true;    
        }
        
        //  Bubble Sort Algorithm
        public static String sortAsc(String value){        
            char[] c = value.toLowerCase().toCharArray();
            for (int i=0;i<c.length-1;i++){
                for (int j=0;j<c.length-1-i;j++){
                    if(c[j]> c[j+1]){
                        char temp = c[j];
                        c[j] = c[j+1];
                        c[j+1] = temp;
                    }
                }
            }
            return new String(c);
        }