Contents

Count between x (with.Java)

   Oct 21, 2023     5 min read

This is a post about the number between ### and x.

We’ll do this by solving a coding test problem, reflecting on the problem we solved, and exploring other ways to solve it.

Let’s start with the problem

Problem

You are given a string myString.

Complete a solution function that, when myString is divided by the character “x”, returns an array that stores the length of each of the divided strings in order.

Example input and output

If you divide the string by “x”, you get [“o”, “oo”, “o”, “”, “o”, “”].

If we create an array with the lengths of each, it is [1, 2, 1, 0, 1, 0], so we return [1, 2, 1, 0, 1, 0].

My solution to the problem

import java.util.*;
class Solution {
    public int[] solution(String myString) {
        int[] answer;
        char[] c = new char[myString.length()];
        ArrayList<Integer> flag = new ArrayList<Integer>();
        for(int i = 0; i < myString.length(); i++){
            if(myString.charAt(i) == 'x'){
                flag.add(i);
            }
        }

        if(flag.get(flag.size() - 1) == myString.length() - 1 || flag.get(0) == 0){
            answer = new int[flag.size() + 1];
        } else {
            answer = new int[flag.size()];
        }

        for(int i = 0; i < flag.size(); i++){
            System.out.println(flag.get(i));
            if(i == 0){
                answer[i] = flag.get(i);
            }else{
                answer[i] = flag.get(i) - flag.get(i-1) - 1;
            }
        }
        answer[answer.length - 1] = myString.length() - 1 - flag.get(flag.size() - 1);
        return answer;
    }
}
Solution Explained

Initially, I solved the test problem with the above code, but it didn’t pass the other test cases. Here’s my thought on why it didn’t pass.

  • Given a string myString, we need to split the string based on “x” and store the length of each substring.
  • The code tries to find the position of the “x” and store it in the flag list, and calculate the length of each substring based on it. However, the position the code stores in the flag list represents the index of the “x”, not the position of the “x” character. Therefore, when calculating the length of each substring, there will be a difference between the position of the “x” and the index, resulting in unexpected results.
  • For example, in the string “oxooxoxoxxox”, the position of “x” is [1, 3, 5, 6, 8], but the code stores the index of “x”, [1, 2, 4, 5, 7], in the flag list, not this position.
  • This results in an unexpected result like [1, 2, 1, 0, 2, 0] because when calculating the length of the substring, it looks for the difference between the indices.

This means that to fix the problem, you need to divide the string based on “x” and calculate the length of each substring, not the position of “x”.

Below is the self-diagnosed and newly written code.

Newly written code
import java.util.*;

class Solution {
    public int[] solution(String myString) {
        ArrayList<Integer> lengths = new ArrayList<Integer>();
        int length = 0;

        for (int i = 0; i < myString.length(); i++) {
            if (myString.charAt(i) == 'x') {
                lengths.add(length);
                length = 0;
            } else {
                length++;
            }
        }

        lengths.add(length); // add the length of the last substring

        int[] answer = new int[lengths.size()];
        for (int i = 0; i < lengths.size(); i++) {
            answer[i] = lengths.get(i);
        }

    } return answer;
}
Modified code snippet

ArrayList lengths = new ArrayList();: Create a list lengths to store the length of the substring.

int length = 0;: Initialize the variable length to store the length of the current substring.

for (int i = 0; i < myString.length(); i++) { … }: Starts a loop that traverses the given string, myString, character by character.

if (myString.charAt(i) == ‘x’) { … }: Checks to see if the current character is “x”. If it is “x”, add the length of the substring up to this point to the lengths list and initialize the length variable to 0.

If we encounter a non-“x” string, we increment the length value by 1.

Traverse the string through the loop, correctly calculating the length of the substring relative to “x” and storing it in the lengths list.

lengths.add(length);: Add the length of the last substring to the lengths list.

int[] answer = new int[lengths.size()];: Create the result array answer based on the lengths list with a fixed length.

for (int i = 0; i < lengths.size(); i++) { … }: Copy the length of the substring stored in the lengths list into the answer array.

The function terminates and returns the answer array. This array stores the length of each substring, divided by “x”, in order.