Lapindromes: LAPIN

Lapindromes Codechef Solution in C, C++, JAVA and Python

Public Submission link: Click here (anyone can submit here )

CU Submission link: Click here (need login by CU account)


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

Sample Input 1 

6
gaga
abcde
rotor
xyzxy
abbaab
ababc

Sample Output 1 

YES
NO
YES
YES
NO
NO

Lapindromes Codechef Solution in C

#include <stdio.h>

int main(void) {
	// your code goes here
	int t,l,n,c;
	char a[1000];
	scanf("%d",&t);
	while(t--)
	{
	    scanf("%s",&a);
	    c=0;
	    l=strlen(a);
	    if(l%2==0)
	    {
	        n=l/2;
	    }
	    else
	    n=l/2+1;
	    for(int i=0;i < l/2;i++)
	    {
	        for(int j=n;j < l;j++)
	        {
	            if(a[i]==a[j])
	            {
	                c++;
	                a[j]=l/2;
	                break;
	            }
	        }
	    }
	    if(c==l/2)
	    printf("YES\n");
	    else
	    printf("NO\n");
	}
	return 0;
}

Lapindromes Codechef Solution in C++ 17

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

#define input_for(arr1, n)    \
  for (int i = 0; i < n; i++) \
  cin >> arr1[i]
#define output_for(arr1, n)   \
  for (int i = 0; i < n; i++) \
  cout << arr[i] << ' '
bool check_prime(int n)
{
  bool is_prime = true;

  if (n == 0 || n == 1)
  {
    is_prime = false;
  }

  for (int i = 2; i <= n / 2; ++i)
  {
    if (n % i == 0)
    {
      is_prime = false;
      break;
    }
  }

  return is_prime;
}
void solve()
{
  string s;
  cin >> s;
  int hash[26] {0};
  if (s.length() % 2 == 0)
  {
    for (int i = 0; i < s.length() / 2; i++)
    {
      hash[s[i] - 97]++;
    }
    for (int i = s.length()/2; i < s.length(); i++)
    {
      hash[s[i] - 97]--;
    }
    int flag = 0;
    for (int i = 0; i < 26; i++)
    {
      if (hash[i] != 0)
        flag = 1;
    }
    if (flag == 1)
      cout << "NO" << endl;
    else
      cout << "YES" << endl;
  }
  else
  {
    for (int i = 0; i < s.length() / 2; i++)
    {
      hash[s[i] - 97]++;
    }
    for (int i = s.length()/2+1; i < s.length(); i++)
    {
      hash[s[i] - 97]--;
    }
    int flag = 0;
    for (int i = 0; i < 26; i++)
    {
      if (hash[i] != 0)
        flag = 1;
    }
    if (flag == 1)
      cout << "NO" << endl;
    else
      cout << "YES" << endl;
  }
}
int main()
{
  int t;
  cin >> t;
  while (t--)
  {
    solve();
  }
  return 0;
}

Lapindromes Codechef Solution in Python 3


for _ in range(int(input())):
    s=input()
    n=len(s)
    if n%2==0:
        l=s[:n//2]
        r=s[n//2:n]
    else:
        l=s[:n//2]
        r=s[n//2+1:n]


    if sorted(l)==sorted(r):
        print("YES")
    else:
        print("NO")

Lapindromes Codechef Solution in Java

import java.util.*;
import java.lang.*;
import java.io.*;
class lapindrome
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner kk=new Scanner(System.in);
		int testcase=kk.nextInt();
		kk.nextLine();
		while(testcase-->0)
		{
		   String s=kk.next();
		   int flag=0;
		   int n1,n2;
		   int n=s.length();
		   int[] a=new int[26];
		   int[] b=new int[26];
		   if(n%2==0)
		   n1=n/2;//6/2=3
		   else
		   n1=(n/2)+1;

		   for(int i=0;i < n/2;i++)
		   a[s.charAt(i)-97]++;
		   for(int i=n-1;i>=n1;i--)
		   b[s.charAt(i)-97]++;
		   for(int i=0;i < 26;i++)
		   {
		       if(a[i]!=b[i])
		       {
		           flag++;
		           break;
		       }
              // System.out.println(a[i]+" "+b[i]);
		   }
		   if(flag==0)
		   System.out.println("YES");
		   else
		   System.out.println("NO");
		}
	}
}


Lapindromes Codechef Solution
See also  Testing Robot : TSTROBOT