Alternating Letters

On the long long road to Mordor, Sam and Frodo get to talking . . .

Sam: “Mr. Frodo, I am quite exasperated with our situation.”

Frodo: “Sam, that is an interesting choice of words. Note that in the word ‘exasperated’ the letters ‘e’ and ‘a’ alternate 5 times.”

e x a s p e r a t e d

↑ ↑ ↑ ↑ ↑

Sam: “Dear Mr. Frodo. We are starving and being hunted by nasty orcs and giant spiders, I find your sudden interest in spelling most counterintuitive.”

Frodo: “My goodness Sam! You just topped yourself. The word ‘counterintuitive’ has fully 6 alternations, involving the letters ‘t’ and ‘i’ !”

c o u n t e r i n t u i t i v e

↑ ↑ ↑ ↑ ↑ ↑

Sam: “Master Frodo, we are starving. I want to go home! Please stop talking about such insignificant matters, or I will surely turn around and quit this instant.”

Frodo: “Oh my goodness Sam, you just did it again. The word ‘insignificant’ also has 6 alternations!”

i n s i g n i f i c a n t

↑ ↑ ↑ ↑ ↑ ↑

Sam: “That does it. I’m heading back to the shire. You can keep the blasted ring.”

Smeagol: “Sssilly hobbitssess.”

Frodo loves to find alternating patterns of letters in words. An alternation is defined to be two

distinct characters (letters, digits, or punctuation) that alternate at least three times as the word is read from left to right, as shown in the above examples. Thus, the word “boo” has no alternations, but “booboo” has “bobo” as an alternation of length 4. Upper and lower case characters are considered to be different, and so, “aragorn” contains the 4-element alternation “arar”, but “Aragorn” only has the 3-element alternation “rar”. Digits and punctuation may be used, so “a2b-c2d-” has the alternation “2-2-”. The objective of this problem is to write a program to find the length of the longest alternation in a word.

 

Input Format

Your program will read in a series of strings, one per line. Each string will contain no embedded spaces, but may contain punctuation symbols and digits.

 

Output Format

Your program will output the word and the length of the longest alternation. (It does not need to

output the actual letters.) If there is no alternation of length 3 or more, then it outputs 0 as the length. See the example below.

 

Example

Input:                                     Output:

exasperated                exasperated has 5 alternations

counterintuitive          counterintuitive has 6 alternations

insignificant                insignificant has 6 alternations

Aragorn                       Aragorn has 3 alternations

a2b-c2d-                      a2b-c2d- has 4 alternations

cat                               cat has 0 alternations

ca                                 ca has 0 alternations

                                  c has 0 alternations

using System;

using System.Collections.Generic;

using System.Text;

using System.Linq;

 

namespace ConsoleApplication2

{

    class Program

    {

        static void Main(string[] args)

        {

            string word = "exasperated";

            char[] chArray = word.ToCharArray().Distinct().ToArray();

            int maxAlternation = 0;

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

            {

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

                {

                    int tempLength = GetAlternationLength(word, chArray[i], chArray[j]);

                    if (maxAlternation < tempLength)

                    {

                        maxAlternation = tempLength;

                    }

                }

            }

            Console.WriteLine(word + " has " + maxAlternation.ToString() + " alternations");

        }

        public static int GetAlternationLength(string word, char first, char second)

        {

            int flag = 0;

            int count1 = 0;

            int count2 = 0;

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

            {

                if (flag == 0 && word[i] == first)

                {

                    count1++;

                    flag = 1;

                }

                else if (flag == 1 && word[i] == second)

                {

                    count1++;

                    flag = 0;

                }

            }

            flag = 0;

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

            {

                if (flag == 0 && word[i] == second)

                {

                    count2++;

                    flag = 1;

                }

                else if (flag == 1 && word[i] == first)

                {

                    count2++;

                    flag = 0;

                }

            }

            if (Math.Max(count1, count2) > 2)

            {

                return (Math.Max(count1, count2));

            }

            else

            {

                return 0;

            }

        }

    }

}

exasperated has 5 alternations
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