Lapindromes Codechef Solution

Hello, coders today we will be solving Lapindromes CodeChef Solution which is a part of the CodeChef DSA Learning Series

Lapindromes Codechef Solution
Lapindromes Codechef Solution

Problem

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

Sample Input

6
gaga
abcde
rotor
xyzxy
abbaab
ababc

Sample Output

YES
NO
YES
YES
NO
NO

Lapindromes Codechef Solution in Python

T = int(input())
for i in range(T):
    n = input()
    l = len(n)
    if l % 2 == 0:
        b, c = n[:l//2], n[l//2:]
    else:
        b, c = n[:l//2], n[l//2+1:]
    if sorted(b) == sorted(c):
        print("YES")
    else:
        print("NO")

Lapindromes Codechef Solution in CPP

#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
    /*
    *   Used to take input from input.txt
    *   and write output to output.txt
    */
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    // * Initializing the variables
    int numberOfTestCases, freq[150] = {0}, i, length;
    string s;
    bool isLapindrome;
    // * Accepting the number of test cases
    cin>>numberOfTestCases;
    // * Executing each test case one by one
    while(numberOfTestCases--) {
        // * Accepting the string for the current test case
        cin>>s;
        // * Calculating the length of the string
        length = s.length();
        // * Assuming that the current string is lapindrome
        isLapindrome = true;
        /*
        *   Incrementing the frequency array for the left half
        *   of the current string
        */
        for(i=0; i<length/2; i++) {
            freq[s[i]-'a']++;
        }
        /*
        *   Decrementing the frequency array for the right half
        *   of the current string
        */
        for(i=(length+1)/2; i<length; i++) {
            freq[s[i]-'a']--;
        }
        // * Iterating the frequency array
        for(i=0; i<26; i++) {
            /*
            *   If the frequency at any index is greater than 0,
            *   the current string is not a lapindrome
            */
            if(freq[i]>0) {
                isLapindrome = false;
            }
            // * Resetting the frequency array for next test case
            freq[i] = 0;
        }
        // * If the current string is a lapindrome, print "YES"
        if(isLapindrome) {
            cout<<"YES"<<endl;
        }
        // * Otherwise print "NO"
        else {
            cout<<"NO"<<endl;
        }
    }
}

Lapindromes Codechef Solution in JAVA

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 sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t--!=0)
        {
            String S = sc.next();
            int len = S.length();
            String half1="",half2="";
            int c=0;
            half1=S.substring(0,len/2);
            if(len%2==0) half2=S.substring(len/2);
            else if(len%2!=0) half2=S.substring(len/2+1);
            char h1[] = half1.toCharArray();
            char h2[] = half2.toCharArray();
            Arrays.sort(h1);
            Arrays.sort(h2);
    String a = String.copyValueOf(h1);
    String b = String.copyValueOf(h2);
            if(a.equals(b)) System.out.println("YES");
            else System.out.println("NO");
        }
        sc.close();
    }
}

Disclaimer: The above Problem (Lapindromes) is generated by CodeChef but the solution is provided by Chase2learn.This tutorial is only for Educational and Learning purpose.

Sharing Is Caring