Leetcode says wrong answer but independent testing works fine


  • 0
    A

    wrong answer for input {"c","c"}- what is wrong with the program below?
    ....
    class Solution {
    public String longestCommonPrefix(String[] strs) {
    if (strs.length==0 || strs==null) return "";

        int minlength=Integer.MAX_VALUE;
        for(String str:strs){
            if (minlength > str.length()){
                minlength=str.length();
            }
        }
        
        for (int i=0;i<minlength;i++){
            String current = strs[0].substring(0, i+1);
             for (int j=0;j<strs.length;j++){
              if (strs[j].substring(0,i+1)!=current){
                  return strs[j].substring(0,i);
                  }   
             }       	
        }
        return strs[0].substring(0,minlength);
    }
    

    }
    ..


Log in to reply
 

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