Lapindromes Codechef Solution

Hello Programmers In this post, you will know how to solve the Lapindromes CodeChef Solution. Which is a part of the CodeChef DSA Learning Series.

Lapindromes Codechef Solution
Lapindromes Codechef Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Codechef by yourself. If you find any difficulty after trying several times, then you can look for solutions.

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 Solutions 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;
        }
    }
}

Ezoicreport this adLapindromes CodeChef Solutions 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();
    }
}
Ezoicreport this ad

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

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Next: Gross Salary Codechef Solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad