Same Character Pattern Count

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication2

{

    class Program

    {

        static void Main(string[] args)

        {

            string testString = "aabbbaabbbccdd aaabbbccdd aa a";

            List<string> list = new List<string>();

            string tempString = "";

            //Logic to parse the string and populate the list

            for (int i = 0; i < testString.Length; i++)

            {

                if (i < testString.Length – 1)

                {

                    if (testString[i] != ‘ ‘)

                    {

                        if (testString[i] != testString[i + 1])

                        {

                            tempString = tempString + testString[i];

                            list.Add(tempString);

                            tempString = "";

                        }

                        else

                        {

                            tempString = tempString + testString[i];

                        }

                    }

                }

                else

                {

                    if (testString[i] != ‘ ‘)

                    {

                        if (testString.Length > 1)

                        {

                            if (testString[i] == testString[i – 1])

                            {

                                tempString = tempString + testString[i];

                                list.Add(tempString);

                            }

                            else

                            {

                                list.Add(testString[i].ToString());

                            }

                        }

                        else

                        {

                            list.Add(testString[i].ToString());

                        }

                    }

                }

            }

            int count = 0;

            //Calculating the pattern count

            list.Sort();

            for (int i = 0; i < list.Count; i++)

            {

                if (i < list.Count – 1)

                {

                    if (list[i] == list[i + 1])

                    {

                        count++;

                    }

                    else

                    {

                        Console.WriteLine(list[i] + " –> " + (count + 1));

                        count = 0;

                    }

                }

                else

                {

                    Console.WriteLine(list[i] + " –> " + (count + 1));

                }

            }

        }

    }

}
 
a –> 1
aa –> 3
aaa –> 1
bbb –> 3
cc –> 2
dd –> 2
Press any key to continue . . .
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s