Advertisement
svetlai

RepeatingPatternLinq

Nov 29th, 2015
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.94 KB | None | 0 0
  1. namespace RepeatingPrattern
  2. {
  3.     using System;
  4.     using System.Linq;
  5.  
  6.     public class GoogleSolutuion
  7.     {
  8.         public static void Main()
  9.         {
  10.             string input = Console.ReadLine();
  11.  
  12.             var result = FindPattern(input);
  13.  
  14.             if (result == null)
  15.             {
  16.                 result = input;
  17.             }
  18.  
  19.             Console.WriteLine(result);
  20.         }
  21.  
  22.         public static string FindPattern(string text)
  23.         {
  24.             if (text == null)
  25.             {
  26.                 return null;
  27.             }
  28.  
  29.             return Enumerable.Range(1, text.Length / 2)
  30.                 .Where(n => text.Length % n == 0)
  31.                 .Select(n => text.Substring(0, n))
  32.                 .Where(pattern => Enumerable.Range(0, text.Length / pattern.Length)
  33.                     .SelectMany(i => pattern)
  34.                     .SequenceEqual(text))
  35.                 .FirstOrDefault();
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement