Skip to content

Files

Latest commit

 

History

History
48 lines (32 loc) · 1.36 KB

Anagram Difference.md

File metadata and controls

48 lines (32 loc) · 1.36 KB

Screen Shot 2021-12-17 at 15 02 31

Screen Shot 2021-12-17 at 15 02 37

Screen Shot 2021-12-17 at 15 02 43

/*
    Time Complexity : O(N)
    Space Complexity : O(1)

    Where 'N' is the length of the given string.
*/

public class Solution {
	public static int getMinimumAnagramDifference(String str1, String str2) {
		int n = str1.length();

		// Array for storing frequencies.
		int freq[] = new int[26];

		// Store the frequencies of characters of first string.
		for (int i = 0; i < n; i++) {
			freq[str1.charAt(i) - 'a']++;
           		freq[str2.charAt(i) - 'a']--;
		}


	
		// Variable to store the total absolute frequencies difference.
		int freqDiff = 0;

		// Iterating for all alphabets to cacluclate the total absolute frequencies difference.
		for (int i = 0; i < 26; i++) {
            		if(freq[i] > 0) {
                	freqDiff += freq[i];
            	     }	
		}

		// Return minimum manipulations required to make string anagram.
		return freqDiff;
	}
}