I have some random string (letter-case is not matter I suppose) as an example: "aqaswasldkaslfaslyetdop"
.
I need to create a method which takes a string as the argument, then match all substring made from duplicate characters, delete them and return the length of the longest one substring in addition to edited input-string:
“aqaswasldkaslfaslyetdop” —>
int resultLength = GetMaxLengthOfDuplicateSubstring("aqaswasldkaslfaslyetdop", out string resultString);
Console.WriteLine($"{resultString}nTHE LENGTH OF THE LONGEST DELETED SUBSTRING: {resultLength}");
// OUTPUT:
//
// qwkfaslyetdop
// THE LENGTH OF THE LONGEST DELETED SUBSTRING: 4
I have tried to locate a set of characters which are repeat itself.
var example = "qwasldkfaslyetdop";
var matchStrings = new string[example.Length];
var distinctStrings = new List<string>();
for (int index = 0; index < example.Length; index = index + 1)
{
matchStrings[index] = example[index].ToString();
if (Regex.Matches(example, matchStrings[index]).Count > 1)
{
distinctStrings.Add(matchStrings[index]);
}
}
distinctStrings = distinctStrings.Distinct().ToList();
foreach (string element in distinctStrings)
{
Console.WriteLine(element);
}
But due all this permutations like "a"
, "s"
, "l"
, "d"
, "as"
, "sa"
, "asl"
, "sal"
, "sla"
, "lsa"
, "als"
, "las"
, "asld"
, … I just lost my initial idea how solve this problem.
I do not know why teacher from private online school made such difficult exercise, I already lost my ‘deadline’ with it. I have tried also positive lookaheads/lookbehinds, without much of success.
6