Printing the max length substring with max repetition count

using System;

using System.Collections.Generic;

using System.Collections;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            string str = "abcabcabcabc";

            Hashtable ht = new Hashtable();

            //Substring length

            for (int i = 1; i <= str.Length; i++)

            {

                //Substring starting point

                for (int j = 0; j <= str.Length – i; j++)

                {

                    if (ht.ContainsKey(str.Substring(j, i)))

                    {

                        ht[str.Substring(j, i)] = int.Parse(ht[str.Substring(j, i)].ToString()) + 1;

                    }

                    else

                    {

                        ht.Add(str.Substring(j, i), 1);

                    }

                }

            }

            Array words = Array.CreateInstance(typeof(string), ht.Count);

            Array frequency = Array.CreateInstance(typeof(int), ht.Count);

            ht.Keys.CopyTo(words, 0);

            ht.Values.CopyTo(frequency, 0);

            Array.Sort(frequency, words);

            int maxLength = 0;

            for (int i = words.Length – 1; i >= 0; i–)

            {

                if (i == words.Length – 1)

                {

                    maxLength = words.GetValue(i).ToString().Length;

                }

                else

                {

                    if ((int)frequency.GetValue(i) == (int)frequency.GetValue(i + 1))

                    {

                        if (maxLength < words.GetValue(i).ToString().Length)

                        {

                            maxLength = words.GetValue(i).ToString().Length;

                        }

                    }

                    else

                    {

                        break;

                    }

                }

            }

            for (int i = words.Length – 1; i >= 0; i–)

            {

                if (i == words.Length – 1)

                {

                    if (words.GetValue(i).ToString().Length == maxLength)

                    {

                        Console.WriteLine(words.GetValue(i).ToString() + " –> " + (int)frequency.GetValue(i));

                    }

                }

                else

                {

                    if ((int)frequency.GetValue(i) == (int)frequency.GetValue(i + 1))

                    {

                        if (words.GetValue(i).ToString().Length == maxLength)

                        {

                            Console.WriteLine(words.GetValue(i).ToString() + " –> " + (int)frequency.GetValue(i));

                        }

                    }

                    else

                    {

                        break;

                    }

                }

            }

        }

    }

}
 
abc –> 4
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