简体   繁体   中英

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

Example :

  1. If I pass "BAAABA" should return 1, as we see that "A" is repeated immediate 3 times.
  2. When I pass "BAABAA" should return 0, as we don't have any letter repeated immediate 3 times.
  3. when I pass "BBBAAABBAA" should return 2.

Code which I have tried so far:

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

Output:

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

Example :

  1. If I pass "BAAABA" should return 1, as we see that "A" is repeated immediate 3 times.
  2. When I pass "BAABAA" should return 0, as we don't have any letter repeated immediate 3 times.
  3. when I pass "BBBAAABBAA" should return 2.

Code which I have tried so far:

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

Since you've tagged this question with C#, here's a C# solution:

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

[EDIT]

Someone other than the OP has removed the C# tag that was there when I wrote this answer. I'll leave this here anyway, since the code is trivially convertible to Java.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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