Bug in OJ?


  • 0
    N

    I submitted my code to OJ and it failed on test case [""] with exception of "StringIndexOutOfBoundsException". However, this test case passes on my local machine. Here is my code:

    public String longestCommonPrefix(String[] strs) {
    	char cur = '\0';
    	
    	StringBuffer result = new StringBuffer();
    	
    	if (strs.length == 0)
    		return result.toString();
    
    	for (int i = 0; i < strs.length; i++) {
    
    		if (strs[i] == "")
    			return result.toString();
    
    		if (i == 0)
    			cur = strs[i].charAt(0);
    
    		else if (strs[i].charAt(0) != cur)
    			break;
    		
    		if (i == strs.length - 1)
    			result.append(cur);
    		
    		strs[i] = strs[i].substring(1);
    		
    	}
    
    	return result.toString();
    
    }
    

    It returns empty string at the first time entering for loop. I don't understand why it fails. Any suggestions?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.