@@ -0,0 +1,2482 @@
+package algorithms.strings;%0D%0A%0D%0Aimport java.util.*;%0D%0A%0D%0A/*%0D%0A * Problem Statement: %0D%0A * Sid is obsessed with reading short stories. Being a CS student, he is doing%0D%0A * some interesting frequency analysis with the books. He chooses strings S1 and%0D%0A * S2 in such a way that %7Clen(S1) - len(S2)%7C %3C= 1.%0D%0A * %0D%0A * Your task is to help him find the minimum number of characters of the first%0D%0A * string he needs to change to enable him to make it an anagram of the second%0D%0A * string.%0D%0A * %0D%0A * Note: A word x is an anagram of another word y if we can produce y by%0D%0A * rearranging the letters of x.%0D%0A * %0D%0A * Input Format:%0D%0A * The first line will contain an integer, T, representing the number of test%0D%0A * cases. Each test case will contain a string having length len(S1)+len(S2),%0D%0A * which will be concatenation of both the strings described above in the%0D%0A * problem.The given string will contain only characters from a to z.%0D%0A * %0D%0A * Output Format:%0D%0A * An integer corresponding to each test case is printed in a different line,%0D%0A * i.e. the number of changes required for each test case. Print -1 if it is not%0D%0A * possible.%0D%0A * %0D%0A * Constraints: %0D%0A * 1 %3C= T %3C= 100%0D%0A * 1 %3C= len(S1)+len(S2) %3C= 104%0D%0A * %0D%0A * Sample Input: %0D%0A * 6%0D%0A * aaabbb%0D%0A * ab%0D%0A * abc%0D%0A * mnop%0D%0A * xyyx%0D%0A * xaxbbbxx%0D%0A * %0D%0A * Sample Output: %0D%0A * 3%0D%0A * 1%0D%0A * -1%0D%0A * 2%0D%0A * 0%0D%0A * 1%0D%0A */%0D%0Apublic class Anagrams %7B%0D%0A%0D%0A    public static void main(String%5B%5D args) %7B%0D%0A        %0D%0A        Scanner sc = new Scanner(System.in);%0D%0A        %0D%0A        int numCases = sc.nextInt();%0D%0A        %0D%0A        for (int i = 0; i %3C numCases; i++) %7B%0D%0A            String input = sc.next();%0D%0A            System.out.println(minCharsChangedForAnagram(input));%0D%0A        %7D%0D%0A        sc.close();%0D%0A    %7D%0D%0A    %0D%0A    private static int minCharsChangedForAnagram(String str) %7B%0D%0A        %0D%0A        if (str.length() %25 2 == 1) %7B%0D%0A            return -1;%0D%0A        %7D%0D%0A        %0D%0A        String s1 = str.substring(0, str.length() / 2);%0D%0A        String s2 = str.substring(str.length() / 2);%0D%0A        %0D%0A        int charsRequiredToChange = 0;%0D%0A        %0D%0A        int%5B%5D chars1 = new int%5B26%5D;%0D%0A        int%5B%5D chars2 = new int%5B26%5D;%0D%0A        %0D%0A        for (int i = 0; i %3C s1.length(); i++) %7B%0D%0A            chars1%5Bs1.charAt(i) - 97%5D++;%0D%0A            chars2%5Bs2.charAt(i) - 97%5D++;%0D%0A        %7D%0D%0A        %0D%0A        for (int j = 0; j %3C 26; j++) %7B%0D%0A            charsRequiredToChange += (chars1%5Bj%5D - chars2%5Bj%5D %3E 0 ? chars1%5Bj%5D - chars2%5Bj%5D : 0);%0D%0A        %7D%0D%0A        return charsRequiredToChange;%0D%0A    %7D%0D%0A%7D

Add Anagrams