如何检查字符串是否有两个以上的重复字符

iro*_*man 3 c# string duplicates

我正在尝试检查字符串是否包含两个以上的重复字符.

例如

'aabcd123' = ok
'aaabcd123' = not ok
'aabbab11!@' = ok
'aabbbac123!' = not ok
Run Code Online (Sandbox Code Playgroud)

我尝试过这样的事,但没有运气

if (string.Distinct().Count() > 2){ 
                    //do something
                }
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激.

D S*_*ley 12

这个对我有用:

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
Run Code Online (Sandbox Code Playgroud)


Abb*_*bas 8

只为经典循环着想:

public bool HasRepeatingChars(string str)
{
    for(int i = 0; i < str.Length - 2; i++)
        if(str[i] == str[i+1] && str[i] == str[i+2])
            return true;
    return false;
}
Run Code Online (Sandbox Code Playgroud)