Strings: Making Anagrams

  • + 0 comments

    Java with comments

        public static int makeAnagram(String a, String b) {
        // Write your code here
        // simply adding and deleting might delete duplicate values from our second word 
        
    
        int[] alphabet = new int[26];
        
        //count up for characters in our first word
        for(int i = 0; i < a.length(); i++){
            int index = Math.abs(a.charAt(i)-'a');
            alphabet[index]++; 
        }
        
        //the subtract for charcters in our second word, duplicate characters will show as negative numbers
        for(int j = 0;  j< b.length(); j++){
            int indexB = Math.abs(b.charAt(j)-'a');
            alphabet[indexB]--;
        }
        
        int differences = 0; 
        //ignore the sign of the integer to get a count
        for(int count : alphabet){
            differences += Math.abs(count);
        }
        
        return differences; 
        }
    
    }