应返回一个字符串,在一个字符串中连续重复3次,不使用正则表达式…仅使用核心概念

问题描述 投票:0回答:4

示例:

  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);
    }
}
java resolution puzzle resolver
4个回答
1
投票
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

0
投票

执行以下操作:

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

        for (int matchThisTo = 0; matchThisTo < iteratingArray.length - 2; matchThisTo++) {
            for (int thisMatch = 1; thisMatch < 3; thisMatch++) {
                if (matchingindex == 3) {
                    matchingindex = 0;
                    result = result + 1;
                }
                if (iteratingArray[matchThisTo] == iteratingArray[matchThisTo + thisMatch]) {
                    matchingindex = matchingindex + 1;
                }
            }
        }
        System.out.println(result);
    }
}

Demo:

public class Coddersclub {
    public static void main(String[] args) {
        // Sample inputs
        String[] inputs = { "BAAABA", "BAABAA", "BBBAAABBAA", "CXXXBYYYAZZZAAAB" };
        for (String input : inputs) {
            System.out.println(countTriplets(input));
        }
    }

    static int countTriplets(String input) {
        int result = 0;
        int matchingindex = 0;
        char[] iteratingArray = input.toCharArray();

        for (int matchThisTo = 0; matchThisTo < iteratingArray.length - 2; matchThisTo++) {
            for (int thisMatch = 1; thisMatch < 3; thisMatch++) {
                if (matchingindex == 3) {
                    matchingindex = 0;
                    result = result + 1;
                }
                if (iteratingArray[matchThisTo] == iteratingArray[matchThisTo + thisMatch]) {
                    matchingindex = matchingindex + 1;
                }
            }
        }
        return result;
    }
}

输出:

1
0
2
4

0
投票

您的初始代码大部分是正确的,您的逻辑也不错。您只需要在else语句上移动break语句:

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

根据Java代码约定,我还将ThisMatch重命名为小写:thisMatch

稍后编辑:如果您希望'aaaa'返回1,则必须将matchThisTo更新为最后找到的索引。

if (matchingindex == 3) {
    matchingindex = 0;
    result = result + 1;
    // added this line to your initial code 
    matchThisTo = ThisMatch;
}

进展顺利。


0
投票

由于您已经用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。

© www.soinside.com 2019 - 2024. All rights reserved.