I want to accept only strings that do not have any substring repeated three times consecutively in them. The substring is not known in advance. For example, "a4a4a4123" contains "a4"; "abcdwwwabcd" - "w"; "abcde" - valid, no triple repeats.
I tried to implement it myself, but this only works for substrings with one letter:
public bool IsValid(string password)
{
var validate = true;
char lastLetter = ' ';
var count = 1;
for (int pos = 0; pos < password.Length; pos++)
{
if (password[pos] == lastLetter)
{
count++;
if (count > 2)
{
validate = false;
break;
}
}
else
{
lastLetter = password[pos];
count = 1;
}
}
return validate;
}