0

I'm writing with JAVA, and I wrote a recursive boolean function that gets 2 strings and is supposed to return if the first one is prefix of the second one. My code:

public static boolean prefix(String s, String t)
{
    int i = s.length()-1;
    if (s.charAt(i) == t.charAt(i) && s.length() >= 0)
    {
        return true;
    }
    return false;
}

If I'm writing in the main for example:

s = "Del";
f = "Delight";

The function is working as well.

But if I'm writing s = "Dellll", f = "Dell", it says "Out of bounds". Why?

The second thing is that it's not working with big and small characters. For example:

s ="Dell"
f ="dell" 

It will return true for the above.

Thank you.

2
  • 4
    This does not look recursive? Also you use s.length()-1 to get the last character of s and t however t may be shorter, in which case it will throw IndexOutOfBoundsException Commented Dec 20, 2018 at 15:45
  • 2
    If you want to check a prefix, why not just use startsWith method? Commented Dec 20, 2018 at 15:47

3 Answers 3

3

What about using out-of-box solution?

public static boolean prefix(String str, String prefix) {
    return str.startsWith(prefix);
}

In case you really need recursion:

public static boolean prefix(String str, String prefix) {
    if (str == null || str.isEmpty())
        return prefix == null || prefix.isEmpty();
    if (prefix == null || prefix.isEmpty())
        return true;
    if (str.charAt(0) != prefix.charAt(0))
        return false;
    // remove first character of each string and go to next iteration
    return prefix(str.substring(1), prefix.substring(1));
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! I used the 2nd code. Its not working as well.
@liemM which test case is not working for my solution?
If srt = "sssss", prefix = "f" it will work, one problem solved. But if str = "Dell", prefix = "Dell" -> It will return false
2

This is because you declare i as:

int i = s.length()-1;

And then call the ith char in both Strings

if (s.charAt(i) == t.charAt(i) && s.length() >= 0)

But if the first String is bigger than the second then i will be out of bounds. An easier way to do this would be to use the built in method startsWith, which:

Tests if this string starts with the specified prefix.

So your method could be as simple as:

return t.startsWith(s);

Or if you can't use built in methods, then you'll need to check if the first String is larger that the second:

if (s.charAt(i) == t.charAt(i) && s.length() >= 0 && s.length() < t.length())

Comments

0

Out of bounds means that, being the second string shorter than the first one, you are trying to access a character that doesn't exist. Just check the two lengths at the beginning of the function, and switch the parameters internally if needed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.