Friday, 11 November 2016

Lapindromes

Problem Statement

Lapindrome is defined as a string which when split in the middle, gives two halves having the same characters and same frequency of each character. If there are odd number of characters in the string, we ignore the middle character and check for lapindrome. For example gaga is a lapindrome, since the two halves ga and ga have the same characters with same frequency. Also, abccabrotor and xyzxy are a few examples of lapindromes. Note that abbaab is NOT a lapindrome. The two halves contain the same characters but their frequencies do not match. 
Your task is simple. Given a string, you need to tell if it is a lapindrome.

Input:

First line of input contains a single integer T, the number of test cases.
Each test is a single line containing a string S composed of only lowercase English alphabet.

Output:

For each test case, output on a separate line: "YES" if the string is a lapindrome and "NO" if it is not.

Constraints:

  • 1 ≤ T ≤ 100
  • 2 ≤ |S| ≤ 1000, where |S| denotes the length of S

Example:

Input:
6
gaga
abcde
rotor
xyzxy
abbaab
ababc


Output:
YES
NO
YES
YES
NO
NO

Problem Link: lapindromes


The idea:  

Question's intention : To check whether each half of the string is an anagram of the other.

Concept 1: Two anagrams .Anagrams have same characters repeated same number of times. Therefore when they are 
sorted they lead to same arrangement.
For example : 'mad' and 'dam' are anagrams  so they form 'adm' when arranged. 

Concept 2: We need to take out two parts of the string to compare it.

The Code:

#include <iostream>
#include<bits/stdc++.h>
using namespace std;

int main() 
{
 
 int T;
 scanf("%d",&T);
 while(T--)
 {
     string input;
     cin>>input;
     int n=input.length();
     if(n%2==0)
     {
         string a="";
         string b="";
         for(int i=0;i<n/2;i++)
         {
             a+=input[i];
         }
         for(int i=n/2;i<n;i++)
         {
             b+=input[i];
         }
         sort(a.begin(),a.end());
         sort(b.begin(),b.end());
         if(a==b)
         {
             cout<<"YES"<<endl;
         }
         else
         {
             cout<<"NO"<<endl;
         }
         
     }
     else
     {
         string a="";
         string b="";
         for(int i=0;i<(n/2);i++)
         {
             a+=input[i];
         }
         for(int j=(n/2+1);j<n;j++)
         {
             b+=input[j];
         }
         sort(a.begin(),a.end());
         sort(b.begin(),b.end());
         if(a==b)
         {
             cout<<"YES"<<endl;
         }
         else
         {
             cout<<"NO"<<endl;
         }
         
         
         
         
     }
     
 }
 
 
 
 return 0;
}





No comments:

Post a Comment