how to check if string has more than two repeating characters

c#, duplicates, string

Solution

This one worked for me:

public bool  IsOK(string s)
{
  if(s.Length < 3) return true;

  return !s.Where((c,i)=> i >= 2 && s[i-1] == c && s[i-2] == c).Any();
}

'aabcd123'     : OK
'aaabcd123'    : not OK
'aabbab11!@'   : OK
'aabbbac123!'  : not OK

Problem

I'm trying to check if a string contains more than two repeating characters. for example ``` 'aabcd123' = ok 'aaabcd123' = not ok 'aabbab11!@' = ok 'aabbbac123!' = not ok ``` I've tried something like this but with no luck ``` if (string.Distinct().Count() > 2){ //do something } ``` any help would be appreciated.

Original source

Related problems