繁体   English   中英

应返回字符串中连续重复 3 次的字母数,不使用正则表达式...仅使用核心概念

[英]Should return how many letters repeated consecutively 3 times in a string,without using regex...use only core concepts

例子 :

  1. 如果我通过"BAAABA"应该返回1,因为我们看到"A"立即重复3次。
  2. 当我通过"BAABAA"应该返回0,因为我们没有立即重复3次的任何字母。
  3. 当我通过"BBBAAABBAA"应该返回 2。

到目前为止我尝试过的代码:

class Coddersclub {
    public static void main(String[] args) throws java.lang.Exception {
        String input = "Your String";
        int result = 0;
        int matchingindex = 0;
        char[] iteratingArray = input.toCharArray();

        for (int matchThisTo = 0; matchThisTo < iteratingArray.length; matchThisTo++) {
            for (int ThisMatch = matchThisTo; ThisMatch < iteratingArray.length; ThisMatch++) {
                if (matchingindex == 3) {
                    matchingindex = 0;
                    result = result + 1;
                }

                if (iteratingArray[matchThisTo] == iteratingArray[ThisMatch]) {
                    matchingindex = matchingindex + 1;
                    break;
                } else {
                    matchingindex = 0;
                }

            }
        }
        System.out.println(result);
    }
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SOTest {

    final static String regex = "(\\w)\\1*";

    public static void main(String[] args) {
        final String inputString = "aaabbcccaaa";

        final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
        final  Matcher matcher = pattern.matcher(inputString);
        int counter=0;
        while (matcher.find()) {
            String group = matcher.group(0);
            if(group.length()==3) {
                counter++;
                System.out.println("Group found :: "+group);
            }           
        }
        System.out.println("Total pattern count :: "+counter);
    }
}

输出:

Group found :: aaa
Group found :: ccc
Group found :: aaa
Total pattern count :: 3

例子 :

  1. 如果我通过"BAAABA"应该返回1,因为我们看到"A"立即重复3次。
  2. 当我通过"BAABAA"应该返回0,因为我们没有立即重复3次的任何字母。
  3. 当我通过"BBBAAABBAA"应该返回 2。

到目前为止我尝试过的代码:

class Coddersclub {
    public static void main(String[] args) throws java.lang.Exception {
        String input = "Your String";
        int result = 0;
        int matchingindex = 0;
        char[] iteratingArray = input.toCharArray();

        for (int matchThisTo = 0; matchThisTo < iteratingArray.length; matchThisTo++) {
            for (int ThisMatch = matchThisTo; ThisMatch < iteratingArray.length; ThisMatch++) {
                if (matchingindex == 3) {
                    matchingindex = 0;
                    result = result + 1;
                }

                if (iteratingArray[matchThisTo] == iteratingArray[ThisMatch]) {
                    matchingindex = matchingindex + 1;
                    break;
                } else {
                    matchingindex = 0;
                }

            }
        }
        System.out.println(result);
    }
}

由于您已使用 C# 标记了此问题,因此这是一个 C# 解决方案:

public static int CountTriples(string text)
{
    int count = 0;

    for (int i = 0; i < text.Length - 2; ++i)
    {
        if (text[i] == text[i+1] && text[i] == text[i+2])
        {
            ++count;
            i += 2;
        }
    }

    return count;
}

[编辑]

OP 以外的其他人删除了我写这个答案时存在的 C# 标记。 无论如何,我将把它留在这里,因为代码可以轻松转换为 Java。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM