Solution for Lapindromes problem - a challenge by CodeChef
CodeChef is a global programming community and every month they conduct coding contest.This month they propose a challenge by the name Lapindromes. In this article we will see my solution for the same.
The Problem Statement
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, abccab, rotor 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.
static void Main(string args)
var input = "gaga".ToCharArray();
int len = input.Length;
var before = input.Take(len / 2).OrderBy(o => o).Aggregate("", (a, b) => a + b);
var after = input.Reverse().Take(len / 2).OrderBy(o => o).Aggregate("", (a, b) => a + b);
if (before == after) Console.WriteLine("It is Lapindrom");
else Console.WriteLine("It is not Lapindrom");
Initially we are converting it into a character array.Then finding the length of the array.In the "before" variable, we are taking the portion of the array that are before the two halves and then performing a sort followed by an aggregate of the strings are converting them.
Similarly, in the "after" variable, we are taking the array portion after the two halves and performing a sort foloowed by joining of the characters into a string.
Finally we are tallying the two halves and if there is a successful match, then it is "Lapindrom" else "it is not".
I hope that this will be a good learning exercise for those whose are beginners in C#.Also participating in challenges helps us to hone our skills.Thanks for reading.
Disclaimer: This solution has been personally prepared by me and submitted for the knowledge sharing purpose.