I have this test challenge but I'm absolutely beginner so I stuck with it, any proper solution would be really appreciated!

We'll say that a "quadruple" in a string is a char appearing four times in a row. Print the number of quadruples in the given string. The quadruples may overlap.

HINT: Watch out for spaces!

Input Format A single line with a string.

Output Format A number.

Sample input abcXXXXXabc

Sample output 2

import java.util.*;

public class Quadruples{
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        char [] c = s.toCharArray();
        int j=0; int k=1; int m=j+2;

        for(int i=0; i<c.length-1; i++){
            if(c[j] != c[k]){
                j++; k++;
            }else{
                for(int l=0; l<c.length-1; l++){
                    if(c[j] == c[m]){
                        m++;
                    }else{   
                        continue;
                    }
                }
            }
        }
    }
}

4 Answers

2
Community On Best Solutions

Very simple brute force approach using a for loop:

    String input = "abcXXXXXabc";

    // Counter to store the number of quadruples found
    int quadrupleCounter = 0;

    // Loop over each character in the input string (skip the last 3 to prevent going out of bounds)
    for(int i = 0;i < input.length() - 3; i++) {
      // Get the current char in the string + the next 3
      char c1 = input.charAt(i);
      char c2 = input.charAt(i + 1);
      char c3 = input.charAt(i + 2);
      char c4 = input.charAt(i + 3);

      // Check if all 4 characters are equal
      if(c1 == c2 && c2 == c3 && c3 == c4) {
        quadrupleCounter++; // Increase the counter
      }
    }

    System.out.println(quadrupleCounter);
1
Hamza Ince On

Not tested, but I think you can test it and correct it, from now on.

public class CheckQuadruple {
    private Boolean checkQuadrupleInString(String str, int i) {
        if (i + 3 >= str.length())
            return false;
       if (str.charAt(i) != str.charAt(i + 1))
           return false;
       if (str.charAt(i + 1) != str.charAt(i + 2))
          return false;
        if (str.charAt(i + 2) != str.charAt(i + 3))
            return false;
        return true;
    }

    public int findQuadruple(String str) {
        int sum = 0;
        for (int i = 0; i < str.length(); i++) {
            if (checkQuadrupleInString(str, i))
                sum += 1;
        }
        return sum;
    }
}
2
Maurice Perry On

Or with a regexp (just for fun):

    Pattern pattern = Pattern.compile("(.)\\1{3}");
    int quadrupleCounter = 0;
    Matcher matcher = pattern.matcher(input);
    if (matcher.find()) {
        do {
            quadrupleCounter++
        } while (matcher.find(matcher.start()+1));
    }
1
Community On

So if i undestand it correctly "aaaabcccccc" means that there are 4 quadruples in this strings for example, as you said they can overlap. so that means aaaa is 1, the first four cccc is 2, and then counting from the second c there is another one so thats 3, and then another one counting from the third c so thats 4. If that is so then the following code should be okay.

So we make a String 'stringArray' by splitting the string into characters. Then we use the firs for loop(with i), to iterate through all letters, then within that for loop we make another for loop to check if the current letter is equal to the next 3 letters--->if that is true, then we increment the quadCount(how many quadruples we found). Then when the forloop is done, we print out the quadCount.

You see that we check if j=i+3 is smaller then the length of the String array(in the second forloop), beacuse if there are not 3 more characters till the end of the letterArray after the one that we are checking, then surely there wont be 3 more similar ones aswell.

If watch out for the spaces means that "cc cc" does not have any quadruples in there, then the code below is correct. If that means that you should not consider spaces, and "cc cc" has one quadruples then just call the .trim() method on the String first like so:String quad= "aaaabccc ccc".trim();-->this will delete(trim) all spaces from the String and then the code is good.

public static void main(String[] args) {
    String quad= "aaaabcccccc";
    int quadCount=0;
    String[] stringArray= quad.split("");

    for(int i=0; i<stringArray.length;i++){
        String current= stringArray[i];
        int j=i+3;
        if(j<stringArray.length){

            if(current.equals(stringArray[i+1]) && current.equals(stringArray[i+2]) && current.equals(stringArray[i+3]) ){
                quadCount++;
            }
        }

    }
        System.out.println("Quadcounter final is: "+quadCount);
    }