Click here to Skip to main content
15,936,709 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
You can't use any loops, if-else, try-catch, recursion and ect.
You can overlaps. In the sequence 1111 the longest repetition is 111. In the sequence 11211 - the longest repetition is 11.

LongestRepetition(new [] {1, 2, 1, 2, 1, 2, 3, 2, 1, 2}) should return new [] {1, 2, 1, 2}.

What I have tried:

I don't know what to try. I solved it without LINQ.

that's my code:

static string LongestRepetition(string str)
        {
            var list = new List<string>();
            var counter = 0;
            var rep = "";

            for (int i = 0; i < str.Length; i++)
            {
                rep = "";
                for (int k = i; k < str.Length; k++)
                {
                    counter = 0;
                    rep += str[k];
                    var startIndex = 0;
                    var indexOf = int.MinValue;
                    while ((indexOf = str.IndexOf(rep, startIndex)) >= 0)
                    {
                        counter++;
                        startIndex = indexOf + 1;
                        if (counter > 1) break;
                    }

                    if (counter > 1)
                    {
                        list.Add(rep);
                    }
                }
            }

            return list.OrderByDescending(x => x.Length).FirstOrDefault();
        }
Posted
Updated 6-Mar-18 20:45pm

1 solution

LongestRepetition(new [] {1, 2, 1, 2, 1, 2, 3, 2, 1, 2}) should return new [] {1, 2, 1, 2}.


Why? It should logically return new [] {1, 2, 1, 2, 1, 2}, because it's a longest repetition.

Check this generic linq solution:
C#
public static T[] GetLongestRepetition<T>(T[] list)
{
	
	return list.TakeWhile(o => list.GroupBy(x=>x)
				.Where(grp=>grp.Count()>1)
				.Any(grp=>grp.Key.Equals(o)))
		.ToArray();
}

Usage:
C#
int[] nums = new int[]{1, 2, 1, 2, 1, 2, 3, 2, 1, 2};
var result = GetLongestRepetition(nums);
Console.WriteLine("{0}", string.Join("", result)); //prints 121212
	
string s = "aabaa";
var result1 = GetLongestRepetition(s.ToArray());
Console.WriteLine("{0}", string.Join("", result1)); //prints aa

string num = "1123811238479";
var result2 = GetLongestRepetition(num.ToArray());
Console.WriteLine("{0}", string.Join("", result2)); //prints 1123811238


Note #1: This solution, unfortunatelly, uses a "hidden loop", see: TakeWhile[^]

Note #2: I haven't enough time to test my solution. So, it may need improvements...
 
Share this answer
 
Comments
Graeme_Grant 6-Mar-18 23:01pm    
5'd
Maciej Los 7-Mar-18 2:00am    
Thank you, Graeme.
Member 13711733 7-Mar-18 2:54am    
Thanks! But I can't rework it to do the magic without jumping. Do you think it can be done with input
new [] {1, 2, 1, 2, 1, 2, 3, 2, 1, 2})
to return
new [] {1, 2, 1, 2}
Maciej Los 7-Mar-18 3:00am    
As i mentioned in my answer - NO. The longest repetition is: {1, 2, 1, 2, 1, 2}
Member 13711733 7-Mar-18 9:07am    
In your case if 1212123212 returns 121212, than 11211 should return 111, not 11.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900