Lapindromes

 


Problem link: Codechef

Lapindrome is defined as a string which when split in the middle, gives two halves having the same characters and the same frequency of each character. If there are an 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 the 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:

The 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

Solution:

Using sort method:

def st(s):

    a=sorted(s[:len(s)//2])

    b=sorted(s[len(s)//2:])

    if(a==b):

        print("YES")

    else:

        print("NO")

t=int(input())

while(t>0):

    t-=1

    s=input()

    if len(s)%2==0:

        st(s)

    else:

        s=s.replace(s[len(s)//2],"")

        st(s)


 Using count method:

def st(s):

    a=s[:len(s)//2]

    b=s[len(s)//2:]

    f=1

    for i in range(0,len(a)):

        #print(a[i],a[i] not in b)

        if(a.count(a[i])!=b.count(a[i])or a[i] not in b):

            #print(a.count(a[i]),b.count(a[i]))

            f = 0

            break

     

           

           

    if f==1:

        print("YES")

    else:

        print("NO")

       

t=int(input())

while(t>0):

    t-=1

    s=input()

    if len(s)%2==0:

        st(s)

    else:

        s=s[:len(s)//2]+s[(len(s)//2)+1:]

        #print(s)

        st(s)



Comments

Popular posts from this blog

Trouble with the Number System

Residue Arithmetic

Perfect Peak of Array