Contents

Check if it's a prefix (with.Java)

   Sep 8, 2023     2 min read

This article explains how to check if a prefix is a prefix (with.Java).

We’re going to solve a coding test problem, reflect on the problem we solved, and learn about other ways to solve it.

Let’s start with the problem.

Problem

For any string, a prefix means the string up to a certain index. For example, all prefixes for “banana” are “b”, “ba”, “ban”, “bana”, “banan”, and “banana”.

Given a string my_string and is_prefix, write a solution function that returns 1 if is_prefix is a prefix of my_string and 0 otherwise.

Example input and output

my_string: “banana”

is_prefix: “ban”

result: 1

This returns 1 because is_prefix is the prefix of my_string.

My solution to the problem

import java.util.*;
class Solution {
    public int solution(String my_string, String is_prefix) {
        int answer = 0;
        ArrayList<String> arr = new ArrayList<String>();
        for(int i = my_string.length(); i > 0; i--){
            arr.add(my_string.substring(0,i));
        }

        } answer = arr.contains(is_prefix) ? 1 : 0;
        return answer;
    }
}
Solution Explained

In the previous post, the suffix was solved by creating a string containing the last character one by one and storing it in an array. This time, the problem is with the prefix.

If we look at substing() again, we see that when we enter only one param, it is recognized as startIndex, and it returns a value of the form (startIndex, string length).

Since we’re creating a prefix this time, the startIndex should always be 0, and the last indexes should be decremented by one.

To match that logic, we’ve set the initial value to my_string.length() to traverse the loop and stop traversal when i reaches zero.

The reason for this is that substring(0,0) will return nothing, so if the loop is traversed with a zero in it, an empty string will be included in arr and allocated useless memory.

We then decrement i by 1 and have it return 0 if any of the elements in arr stored in answer have the same value as is_prefix, and 1 if none.