• + 0 comments

    Java 8 This is one of the easiest way to solve this problem.

     public static int anagram(String s) {
            int n = s.length();
            
            if(n % 2 != 0){
                return -1;
            }
            
            //Split the main string
            String left = s.substring(0, n/2);
            String right = s.substring(n/2, n);
            
            //Iterate the left string to verify if the letters are contained into the 
            // right string
            for(int i = 0; i < n/2; i++){
                if(right.contains(left.substring(i, i + 1))){
                    right = right.replaceFirst(left.substring(i, i + 1), "");
                }
            }
            return right.length();
            
            
    
        }