Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Fórum ➡ Border of a string S with at least 3 non-overlapping solutions ( yield version ) ♦
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 // in reply to the thread // help with a C# program // https://social.msdn.microsoft.com/Forums/WindowsServer/en-us/85db883e-f7ee-4536-b0f1-53753873c80d/help-with-a-c-program?forum=csharpgeneral #define BONUS using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; // aljodav // http://rextester.com/users/376 namespace Rextester { public class Solution{ public int MaxBorderLength(string s){ if(s==default(string)||s.Length<3)throw new ArgumentException("*** Hey!"); #if BONUS Console.WriteLine("\tstring={0}",s); #endif IEnumerable<string> allBorders=_findAllBorders(s).Where(x=>x!=default(string)); if(allBorders.Count()==0)return 0; int maxLength=allBorders.Max(x=>x.Length); IEnumerable<string> theBorder=allBorders.Where(x=>x.Length==maxLength); if(theBorder.Count()!=1){throw new Exception("Oops! (impossible by logic)");} string maxBorder=(theBorder.ToArray())[0]; #if BONUS Console.WriteLine("\tborder={0}\n\tlength={1}",maxBorder,maxBorder.Length); #endif return maxBorder.Length; } private IEnumerable<string> _findAllBorders(string s){ int lenMax=s.Length/3; for(int len=1;len<=lenMax;++len){ string start=s.Substring(0,len); if(s.EndsWith(start)==false){yield return default(string);continue;} int nextBorder=s.IndexOf(start,len),lastBorder=s.LastIndexOf(start); if(lastBorder-nextBorder>=start.Length)yield return start; else yield return default(string); } yield break; } } public class Program { public static void Main(string[] args) { string[] testStrings={ "mamxmamam", "codility", "abc", "kkk", "barbararhubarb", "barbarabarbrhubarb", "xyzaxyzbxyz", "abab", "ababab", "baaab" }; Solution sol=new Solution(); foreach(string ts in testStrings){ Console.WriteLine(sol.MaxBorderLength(ts)); } } } }
run
|
edit
|
history
|
help
0
Code
SpiltStringBetweenSpecialCharacters
IEnumerable<T> generic interface implementation
Trabalho
a
Salario
Fórum ➡ Calculate the Sum of Values in List of KeyValuePair<> objects ♦
Generic Reference constraint
w
cs002