Lapindromes- Solution for coding challenge by CodeChef

Niladri.Biswas
Posted by in C# category on for Beginner level | Points: 250 | Views : 1430 red flag

Solution for Lapindromes problem - a challenge by CodeChef

Introduction

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.

Solution

using System;
using System.Linq;

namespace ConsoleApplication3
{
    class Program
    {
        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");
            
            Console.ReadKey();
        }

        
    }
}

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".

Conclusion

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.

Page copy protected against web site content infringement by Copyscape

About the Author

Niladri.Biswas
Full Name: Niladri Biswas
Member Level: Platinum
Member Status: Member
Member Since: 10/25/2010 11:04:24 AM
Country: India
Best Regards, Niladri Biswas
http://www.dotnetfunda.com
Technical Lead at HCL Technologies

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)