检测字符串是否包含C#中的多个连续重复字符的最有效方法是什么?

检测字符串是否包含C#中的多个连续重复字符的最有效方法是什么?,c#,string,C#,String,例如,一个用户输入“我爱这个帖子 应该检测到连续重复的感叹号“!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!”。我认为更好的方法是创建一个数组,数组中的每个元素负责相邻字符串上的一个字符对,例如第一个aa、bb、cc、dd。这个数组的每个元素上都有0 此问题的解决方法是对该字符串执行for并更新数组值。 接下来,您可以根据需要分析此阵列 例如:对于string:bbaaccccdab,您的结果数组将是{2,1,3},因为'aa'可以找到2次,'bb'可以找到一

例如,一个用户输入“我爱这个帖子


应该检测到连续重复的感叹号“!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!”。

我认为更好的方法是创建一个数组,数组中的每个元素负责相邻字符串上的一个字符对,例如第一个aa、bb、cc、dd。这个数组的每个元素上都有0

此问题的解决方法是对该字符串执行for并更新数组值。 接下来,您可以根据需要分析此阵列

例如:对于string:bbaaccccdab,您的结果数组将是{2,1,3},因为'aa'可以找到2次,'bb'可以找到一次(在字符串开头),'cc'可以找到三次

为什么“抄送”三次?因为'cc'cc&c'cc'c&cc'cc'。

可以在
O(n)
中轻松完成:对于每个字符,如果前一个字符与当前字符相同,则增加一个临时计数。如果不同,请重置临时计数。在每个步骤中,如果需要,请更新您的全局设置

对于
abbcc
,您可以得到:

a => temp = 1, global = 1
b => temp = 1, global = 1
b => temp = 2, global = 2
c => temp = 1, global = 2
c => temp = 2, global = 2
c => temp = 3, global = 3

=> c appears three times. Extend it to get the position, then you should be able to print the "ccc" substring.

您可以很容易地扩展它以获得起始位置,我将把它留给您。

下面的正则表达式将检测重复字符。您可以增加数字或将其限制为特定字符,以使其更健壮

        int threshold = 3;
        string stringToMatch = "thisstringrepeatsss";
        string pattern = "(\\d)\\" + threshold + " + ";
        Regex r = new Regex(pattern);
        Match m = r.Match(stringToMatch);
        while(m.Success)
        {
                Console.WriteLine("character passes threshold " + m.ToString());
                m = m.NextMatch();
         }

这是一个我精心设计的快速解决方案,其中添加了一些额外的副本。正如其他人在评论中指出的那样,一些副本将是完全合法的,因此您可能希望将标准缩小到标点符号,而不仅仅是字符

string input = "I loove this post!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!aa";

int index = -1;
int count =1;
List<string> dupes = new List<string>();

for (int i = 0; i < input.Length-1; i++)
{
    if (input[i] == input[i + 1])
    {
        if (index == -1)
            index = i;

        count++;
    }
    else if (index > -1)
    {
        dupes.Add(input.Substring(index, count));
        index = -1;
        count = 1;
    }
}

if (index > -1)
{
    dupes.Add(input.Substring(index, count));
}
string input=“我喜欢这个帖子!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!aa”;
int指数=-1;
整数计数=1;
列表重复=新列表();
for(int i=0;i-1)
{
Add(input.Substring(index,count));
指数=-1;
计数=1;
}
}
如果(索引>-1)
{
Add(input.Substring(index,count));
}

以下是一个函数的示例,该函数搜索指定长度的连续字符序列,同时忽略空白字符:

    public static bool HasConsecutiveChars(string source, int sequenceLength)
    {
        if (string.IsNullOrEmpty(source))
            return false;
        if (source.Length == 1) 
            return false;

        int charCount = 1;
        for (int i = 0; i < source.Length - 1; i++)
        {
            char c = source[i];
            if (Char.IsWhiteSpace(c))
                continue;
            if (c == source[i+1])
            {
                charCount++;
                if (charCount >= sequenceLength)
                    return true;
            }
            else
                charCount = 1;
        }

        return false;
    }
public static bool hascoutivechars(字符串源,int sequenceLength)
{
if(string.IsNullOrEmpty(源))
返回false;
if(source.Length==1)
返回false;
int charCount=1;
for(int i=0;i=sequenceLength)
返回true;
}
其他的
字符数=1;
}
返回false;
}
编辑固定范围错误:/

使用LINQ!(为了一切,而不仅仅是这个)


你想如何处理“正确”的重复,比如“我喜欢tvanfosson关于糟糕逗号的正确评论?”看到问题了吗?@Glennular——你应该说“我呢?”可能有一些有效的单词有连续的重复。你对最长的连续序列感兴趣吗,所有这样的序列或者只是想知道这样的序列是否存在?一个更现实的例子是“天哪,我爱你的帖子!!!!!!!!!!!!!!!1111”Baa!土豚的仇恨废墟。链球菌或肺炎球菌是如何感染他的尾骨的2,1,3. 诚然,这是胡说八道,但显然在语法上仍然有效。(2,1,3,…,1,…)@AngryHacker您编辑了帖子,但留下了Java类名,C#(BCL)中没有
Matcher
类。您可以对正则表达式使用字符串文字:
“(\\w)\\1+”
-->
@”(\w)\1+“
。同样,这个答案是Java-使用
Regex
Regex.IsMatch
而不是
Pattern
我尝试了这个解决方案,但运气不好。你能纠正我的方法吗<代码>静态bool包含samecharacter(string textToCheck,int maxNumberOfConcertivedUpplicates){string pattern=@“(\w)\”+maxNumberOfConcertivedUpplicates++”;var r=new Regex(pattern);Match m=r.Match(textToCheck);返回m.Success;}
string test = "aabb";
return test.Where((item, index) => index > 0 && item.Equals(test.ElementAt(index)));
// returns "abb", where each of these items has the previous letter before it
string test = "aabb";
return test.Where((item, index) => index > 0 && item.Equals(test.ElementAt(index))).Any();
// returns true