LAPIN - Editorial

Even better would be to just write your code so that this kind of problem simply can’t happen in the first place; none of l,l1,i,s or m are used outside of the while block, so why declare them there?

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

int main() {
    int t;
    cin>>t;
    while(t--)
    {
        int l,l1,i;
        string s;
        map<char,int> m;
        cin>>s;
        l=s.size()/2;
        if(l==0)
        {
            cout<<"NO\n";
            continue;
        }
        if(s.size()%2==0)
        {

            l1=l;
        }
        else
        {
            l1=l+1;
        }
        i=l1;
        while(i<s.size())
        {
            if(m.find(s[i])==m.end())
            {
                m.insert(pair<char,int>(s[i],1));
            }
            else
                m[s[i]]++;
            i++;
        }
        for(i=0;i<l;i++)
        {
            if(m.find(s[i])==m.end()|| m[s[i]]==0)
            {
                cout<<"NO\n";
                break;
            }
            else
                m[s[i]]--;
        }
        if(i==l)
        {
            cout<<"YES\n";
        }
    }
    return 0;
}

or better still:

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

int main() {
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        const int l=s.size()/2;
        if(l==0)
        {
            cout<<"NO\n";
            continue;
        }
        int l1 = l;
        if(s.size()%2!=0)
        {
            l1=l + 1;
        }
        int i=l1;
        map<char,int> m;
        while(i<s.size())
        {
            if(m.find(s[i])==m.end())
            {
                m.insert(pair<char,int>(s[i],1));
            }
            else
                m[s[i]]++;
            i++;
        }
        for(i=0;i<l;i++)
        {
            if(m.find(s[i])==m.end()|| m[s[i]]==0)
            {
                cout<<"NO\n";
                break;
            }
            else
                m[s[i]]--;
        }
        if(i==l)
        {
            cout<<"YES\n";
        }
    }
    return 0;
}
4 Likes

but there may be repeated memory allocation and memory release for them… will it affect performance of code?

The construction of an empty string or an empty map should not involve memory allocations.

1 Like

got it …Thank you sir…

1 Like

whats wrong in this solution

include<bits/stdc++.h>

using namespace std;

int main()
{
int i,j,count=0;
int t;
cin>>t;
while(t–)
{ int n;
string s;
cin>>s;
n=s.length();
if(n%2==0)
{
for(i=0;i<n/2;i++)
{
for(j=n/2;j<n;j++)
{
if(s[i]==s[j])
{
count++;
}
}

						}
						if(count==n/2)
						cout<<"yes"<<endl;
						else
						cout<<"no"<<endl;
				}
				else if(n%2!=0)
					{
						for(i=0;i<n/2;i++)
						{
							for(j=(n+1)/2;j<n;j++)
								{
									if(s[i]==s[j])
									{
										count++;
									}
								}
								
						}
						if(count==n/2)
						cout<<"yes"<<endl;
						else
						cout<<"no"<<endl;
					
					
				}
		}
		return 0;
}

Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

My approach using Counter in python,
Have a look :
https://www.codechef.com/viewsolution/33027780

There must be some issue with the test cases.
I think there is a non-lowercase english alphabet somewhere (the question specifies only lowercase english alphabets are in the input)

I don’t see any other reason why my solution wouldn’t work : https://www.codechef.com/viewsolution/34547143

If someone finds an issue. Please let me know, thanks

https://www.codechef.com/viewsolution/35751966

May I know why the output don’t match

Repllies will be appreciated #CodeChef-DSA-learners #editorial #help #lapindrome

https://www.codechef.com/viewsolution/36790347

I Could’t understand that what’s the error in this code.
If I Put Mannual Input in this code. Its Execute sucessfully and shows the correct output. but when i submitted , this is showing wrong answer.
What should i do?

I have tried different approach . I divide the string into two and add ASCII of both strings. C1 for left ASCII and C2 for right ASCII. if both are same then they are lapindrome. My code passes the test cases But still i am getting wrong answer. Somebody help.

#include
using namespace std;
int islapin(string s){
int len=s.length();
int c1=0,c2=0,l;
bool r=true;
if(len%2 !=0){
r=false;
}
l=len/2;
for(int i=0;i<len;i++){
if(i<l){
c1+=(int)s[i];
}else if(i==l && r==false){
continue;
}else{
c2+=(int)s[i];
}
}

if(c1!=c2){
    return 1;
}else{
    return 0;
}

}
int main(){
int n;
cin>>n;
int a=(int)calloc(n,sizeof(int));
string s;

for(int i=0;i<n;i++){
    cin>>s;
    if(islapin(s)){
        a[i]=1;
    }
}

for(int i=0;i<n;i++){
    if(a[i]==1){
        cout<<"NO"<<endl;
    }else{
        cout<<"YES"<<endl;
    }
}
return 0;

}

Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

Thanks. But I solved it. actually this code adds the ascii values, so on input “Abab” it will print “NO”.This was getting logical errors in some cases. I changed my approach and it was accepted successfully.

1 Like

All the test cases are passed but still , I am getting WA.

#include <iostream>
#include <string>
#include <map>

using namespace std;

int main()
{
	int T;
	string str, answer;
	map<char, int> l,r;
	cin >> T;
	while (T>0)
	{
		cin >> str;
		answer = "yes";
		if (str.length()%2 == 0)
		{
			for(int i=0; i<(str.length()/2); i++)
			{
			
				l[str[i]] += 1;	
			
			}
			for (int j = (str.length()/2); j<str.length(); j++)
			{

				r[str[j]] += 1;	

			}
		}
		else
		{
			str = str.erase(str.length()/2,1);
			//cout << str;
			for(int i=0; i<(str.length()/2); i++)
			{
					l[str[i]] += 1;	
			}
			for (int j = (str.length()/2); j<str.length(); j++)
			{
					r[str[j]] += 1;	
			}
		}
		for (int k=0; k<str.length()/2; k++)
		{
			if (l[str[k]] != r[str[k]])
			{
				answer = "no";
				
				break;
			}
		}
		if (answer == "yes")
		{
			cout << "YES\n"; 
		}		
		else 
		{
			cout << "NO\n";
		}
		T--;
	}
	return 0;
}

Please have a look over my code, i have used map to count the frequency of each character on left hand side and right hand side.

This is my code and I tried many test cases and they all are correct but when I submit my code it shows WA.

/* package codechef; // don’t place package name! */

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
{
int t;

    Scanner scan = new Scanner(System.in);
    t=scan.nextInt();
    while(t>0){
         String st=new String();
    String b=new String();
    String a= new String();
        st=scan.next();
        int l=0;
        l=st.length();
        int n1=0,n2=0;
        int c=0;
        if (l%2==1){
            l=l/2 + 1;
            for (int i=l-1;i<st.length() ;i++ ) {
                   b=b+st.charAt(i)   ;      
            }
            // System.out.println(b);
        }
        else{
            l=l/2;
            for (int i=l;i<st.length() ;i++ ) {
                   b=b+st.charAt(i)   ;      
            }
        }
        for (int i=0;i<l ;i++ ) {
                      a=a+st.charAt(i)  ;      
                }
        for (int i=0;i<a.length() ;i++ ) {
                    n1=(int)(char)a.charAt(i);
                    n2=(int)(char)b.charAt(i);
                   c= n1^n2^c ;      
            }
        // System.out.println(a+"   "+b);
        if (c==0){
            System.out.println("YES");
        }else{
            System.out.println("NO");}
        t-=1;

    }
}

}

`#include
#include
#include
#include
#include
using namespace std;

int main() {
int T;
cin>>T;
//cout<<“T=”<<T<<endl;
vector w;
for(int i=0;i<T;i++)
{
string s;
cin>>s;
w.push_back(s);
}

for(int i=0;i<T;i++)
{
    map<char,int> m;
    string s=w[i];
    
        int flag=0;
        for(int j=0;j<s.length();j++)
        {
            if(j<s.length()/2)
                m[s[j]]++;
            else if(j>=s.length()/2)
            {
            	if(s.length()%2!=0 && j==ceil(s.length()/2))
            		continue;
            	else
            	{
	            		if(m.find(s[j])==m.end())
		                {
		                    cout<<"NO"<<endl;
		                    flag=1;
		                    break;
		                }
		                else
		                {
		                    m[s[j]]--;
		                }
				}
            }
        }
        
        map<char,int>::iterator itr;
        /*for (itr = m.begin(); itr != m.end(); ++itr)

        	cout<<itr->first<<": "<<itr->second<<endl;
		}*/
         for (itr = m.begin(); itr != m.end(); ++itr)
        {
            if(itr->second<0)
            {
                flag=1;
                cout<<"NO"<<endl;
                break;
            }
        }
        if(flag==0)
            cout<<"YES"<<endl;
        //cout<<"hi"<<endl;
	
    
}

return 0;

}
`
I cant figure out whats going wrong. All the test cases are resulting in correct answer including some of my own. Please help

Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

hey guys ,python solution :-

try:
for i in range(int(input())):
a = input()
n = len(a)
l = {}
m={}

    if n % 2 == 0:
        b = a[:n // 2]
        c = a[n // 2:]
    if n % 2 != 0:
        b = a[:(n - 1) // 2]
        c = a[(n + 1) // 2:]
    for i in b:
        d=b.count(i)
        l[i]=d
    for i in c :
        e=c.count(i)
        m[i]=e

    if l==m:
        print("YES")

    else:
        print("NO")

except:
pass

solution
Lapindromes Ad Hoc Problem CodeChef LAPIN - YouTube

solution
Lapindromes Ad Hoc Problem CodeChef LAPIN - YouTube