Problem
Chef is currently working as an intern at Toonpur Health Department. Recently he is assigned the task to identify healthy strings.
A string is considered healthy if, when split into two halves from the middle, the two halves have the same characters and the frequency of each character is also the same. If the string is of odd length, then the middle character is ignored and the string is split into two halves around the middle. For example, string "aefefa" is healthy as the two halves "aef" and "efa" have the same characters and frequency of each character is also the same. On the other hand, the string "defer" is not healthy as in the two halves "de" and "er", characters 'd' and 'r' do not match.
Chef is facing difficulty in identifying which string is healthy. Can you help him with this task?
Input Format
- The first line of the input contains a single integer denoting the number of test cases. The description of test cases follows.
- The first line of each test case contains a single string consisting of lower case English alphabets.
Output Format
For each test case, output in a single line "YES" (without quotes) if the string is healthy or "NO" (without quotes) otherwise.
Constraints
- , where |S| denotes the length of S
Sample 1:
5 lewis madam hofefohe tornado nurses
NO YES YES NO NO
Explanation:
Test Case : This string is unhealthy as the two halves are "le" and "is". The characters in both halves do not match.
Test Case : This string is healthy as the two halves are "ma" and "am". The characters are the same: 'a', 'm'. The character frequency is also same : (a - 1), (m - 1).
Program :
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner se = new Scanner(System.in);
int t = se.nextInt();
while(t--!=0)
{
String S = se.next();
int len = S.length();
String h1="",h2="";
int c=0;
h1=S.substring(0,len/2);
if(len%2==0)
h2=S.substring(len/2);
else if(len%2!=0)
h2=S.substring(len/2+1);
char ha1[] = h1.toCharArray();
char ha2[] = h2.toCharArray();
Arrays.sort(ha1);
Arrays.sort(ha2);
String a = String.copyValueOf(ha1);
String b = String.copyValueOf(ha2);
if(a.equals(b))
System.out.println("YES");
else
System.out.println("NO");
}
se.close();
}
}
0 Comments