应该返回在一个字符串中连续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.HashMap;
import java.util.Map;
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);

        Map<String, Integer> patternToOccuranceCount = new HashMap<String, Integer>();

        while (matcher.find()) {
            String group = matcher.group(0);
            if(group.length()==3)
                if(patternToOccuranceCount.containsKey(group)) {
                    patternToOccuranceCount.put(group, patternToOccuranceCount.get(group)+1);
                }else {
                    patternToOccuranceCount.put(group, 1);
                }
        }
        System.out.println("Input :: "+inputString+", number of pattern found :: "+ patternToOccuranceCount.size()+", :: occurance"+patternToOccuranceCount);
    }
}

输出:

Input :: aaabbcccaaa, number of pattern found :: 2, :: occurance{aaa=2, ccc=1}


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;

    return count;
}

这将返回所有样本的预期结果(当然,很容易将其转换为Java)。

请注意,如果您希望“ AAAAAA”返回1而不是4,那么您必须将代码更改为:

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

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

    return count;
}

如果您想让“ AAAAAA”返回一个2的计数(因为它是两个三元组,那么代码将是:

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

((在这种情况下,“ AAAAAAAA”也将返回2,因为它是两个三个A后面跟一个两个A。)


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

进展顺利。

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