Anagram Hackerrank Solution

package hacker_rank_string;

import java.util.Scanner;

public class Anagram {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
       
        int T = sc.nextInt();
       
        for (int i = 0; i < T; i++) {
            String s = sc.next();
            int angrm = 0;
            if(s.length()%2 != 0){
                System.out.println("-1");
                continue;
            }
            int[] count1 = new int[26];
            int[] count2 = new int[26];
       
            for (int j = 0; j < s.length()/2; j++) {
                count1[Math.abs(97-s.charAt(j))]++;
                count2[Math.abs(97-s.charAt(s.length()-1-j))]++;
            }
            for (int j = 0; j < count1.length; j++) {
                if(count2[j] > count1[j])
                    angrm += (count2[j]-count1[j]);
            }
            System.out.println(angrm);
        }
    }
}

Comments

Popular posts from this blog

Alternating Characters Hackerrank solution

Game of Thrones - I Hackerrank Solution