0
class Solution {
    public String longestCommonPrefix(String[] strs) {    
        String result = new String("");
        char compareElement;
        int i;//index of strs
        int j;//index of the first one of string
        for(j = 0; j < strs[0].length(); j++){
            compareElement = strs[0].charAt(j);
            for(i = 1; i < strs.length; i++){
                if(compareElement == strs[i].charAt(j)){
                    if(i == strs.length - 1)
                        result += compareElement;
                    else
                        continue;
                }
                else{
                    break;
                }
            }
        }

        return result;
    } 
}

Test sample is

Input: ["flower","flow","flight"]
Output: "fl"

hi there I have got a problem with string in Java in my 4th small program in Leetcode. The aim of this function is to find the longest common prefix string amongst an array of strings. But the exception

Exception in thread "main" 
java.lang.StringIndexOutOfBoundsException: String index out of 
range: 4
at java.lang.String.charAt(String.java:614)
at Solution.longestCommonPrefix(Solution.java:11)
at __DriverSolution__.__helper__(__Driver__.java:4)

appears over again. Has someone any idea? Thanks!

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
Zonyue Li
  • 11
  • 2
  • One suggestion: using `j` for the outer loop and `i` for the inner breaks the normal convention and will cause confusion to anyone reading your code. Normally loops start at `i` on the outside and then `j`, `k` (and if you get past `k` you're probably doing it wrong ;-). Actual problem is that your second string has less chars than the first so when you look for character 4 in the 2nd string, it isn't there. – John3136 Dec 08 '18 at 17:34
  • 2
    Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Krease Dec 08 '18 at 17:38

3 Answers3

0

I think this is where you go wrong:

if(compareElement == strs[i].charAt(j))

j can become too large as it goes from 0 to strs[0].lenght() (see your outer loop).

If strs[i].lengt() is smaller than strs[0].length() you get an StringIndexOutOfBoundsException.

MWB
  • 1,830
  • 1
  • 17
  • 38
0

When you iterate through the comparison strings, you're never checking the length of the string you're comparing. In your example the test case flow. The char at index 4 doesn't exist since only indices 0-3 are defined. if(compareElement == strs[i].charAt(j)){ when j is 4 it'll mess up. In order to fix it you have to make sure you're not going past the length of the string. In addition to that look up what a StringBuilder is, for this small of a test case it won't matter however as you go up larger it will.

Matthew Kerian
  • 812
  • 5
  • 18
0

Your code fails if you have an element in the array which is shorter than the first element. You need to check that j is still smaller than the length of the string you're comparing:

    public String longestCommonPrefix(String[] strs) {
    String result = new String("");
    char compareElement;
    int i;// index of strs
    int j;// index of the first one of string
    for (j = 0; j < strs[0].length(); j++) {
        compareElement = strs[0].charAt(j);
        for (i = 1; i < strs.length; i++) {
            if (j < strs[i].length() && compareElement == strs[i].charAt(j)) {
                if (i == strs.length - 1)
                    result += compareElement;
                else
                    continue;
            } else {
                break;
            }
        }
    }

    return result;
}
John Stringer
  • 558
  • 2
  • 6