haydnD
haydnD

Reputation: 2293

how to check if string has more than two repeating characters

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.

Upvotes: 4

Views: 16860

Answers (6)

zardilior
zardilior

Reputation: 2978

Use the regex

(?:.*<letter>.*){2}

For letter b for example you'd use

(?:.*b.*){2}

For all vowels

 (?:.*[aeiou].*){2}

For regex usage on c# refer to https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex?view=net-5.0

Upvotes: 0

Abbas
Abbas

Reputation: 14432

Just for classic loops sake:

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;
}

Upvotes: 8

maxbeaudoin
maxbeaudoin

Reputation: 6976

    [TestMethod]
    public void Test()
    {
        const string sample1 = "aabcd123";
        const string sample2 = "aaabcd123";
        const string sample3 = "aabbab11!@";
        const string sample4 = "aabbbac123!";

        Assert.IsTrue(IsOk(sample1));
        Assert.IsFalse(IsOk(sample2));
        Assert.IsTrue(IsOk(sample3));
        Assert.IsFalse(IsOk(sample4));
    }

    private bool IsOk(string str)
    {
        char? last = null;
        var i = 1;
        foreach (var c in str)
        {
            if (last == c)
            {
                i++;
                if (i > 2) return false;
            }
            else
            {
                i = 1;
            }
            last = c;
        }
        return true;
    }

Upvotes: 1

Nolonar
Nolonar

Reputation: 6122

You can use Regex for this one:

return Regex.IsMatch(inputString, @"(.)\1{2,}", RegexOptions.IgnoreCase)

This checks for any character, then for at least 2 times that same character. Works even with strings like "AaA" and can be modified to find out exactly what those characters are, where they are occurring in the string, and much more (also allows you to replace those substrings with something else)

More information:

http://msdn.microsoft.com/en-us/library/6f7hht7k.aspx

http://msdn.microsoft.com/en-us/library/az24scfc.aspx

Upvotes: 3

D Stanley
D Stanley

Reputation: 152556

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

Upvotes: 12

scottheckel
scottheckel

Reputation: 9244

Since you want to find runs of three characters and not just three characters in a string, you need to loop through and look at three consecutive characters to see if they are the same. A loop like this will work.

string myString = "aaabbcd";
bool hasMoreThanTwoRepeatingCharsInARow = false;
for(int index = 2; index < myString.Length; index++)
{
  if(myString[index] == myString[index - 1] && myString[index] == myString[index - 2])
  {
    hasMoreThanTwoRepeatingCharsInARow = true;
  }
}

I would stick this in a method and make the variables better and you're good to go!

Upvotes: 1

Related Questions