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 ( optimized 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 #define BONUS using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; // aljodav // http://rextester.com/users/376 // an alternative to my 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 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 string theBorder=_findTheBorder(s); if(theBorder==default(string))return 0; #if BONUS Console.WriteLine("\tborder={0}\n\tlength={1}",theBorder,theBorder.Length); #endif return theBorder.Length; } private string _findTheBorder(string s){ int lenMax=s.Length/3; for(int len=lenMax;len>=1;--len){ string start=s.Substring(0,len); if(s.EndsWith(start)==false)continue; int nextBorder=s.IndexOf(start,len),lastBorder=s.LastIndexOf(start); if(lastBorder-nextBorder>=start.Length)return start; } return default(string); } } 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
Fórum ➡ Separating the Fields Inside a String ♦
SunLocation
Problem: on_off
opQ 3
DASX
Recursion
Hex character codes
Subdomain regex
Compress in C#
Sarah Jayne Dunn OnlyFans photos are lea