Home » Java » java – Should return how many letters repeated consecutively 3 times in a string,without using regex…use only core concepts-Exceptionshub

java – Should return how many letters repeated consecutively 3 times in a string,without using regex…use only core concepts-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

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);
    }
}
How to&Answers:
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

Answer:

Do it as follows:

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

Output:

1
0
2
4

Answer:

Your initial code is mostly correct, and your logic is good. You just need to move the break statement on the else statement:

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

I would also rename the ThisMatch to lower case, as per Java code conventions: thisMatch.

Later edit: if you want ‘aaaa’ to return 1, you must update matchThisTo to the last found index.

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

Good job on your progress.

Answer:

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.