Java code quicker than 99.68% code submissions. 8ms solution


  • 0
    B
            int len = strs.length;
            if( len == 0)
                return "";
            if(len == 1)
                return strs[0];
            StringBuffer test = new StringBuffer(strs[0]);
            for(int i=1;i<len;i++){
                if(strs[i].startsWith(test.toString()))
                    continue;
                test.setLength(test.length()-1);
                i--;
            }
            return test.toString();
        }
    

    The only thing that could be tested is that if test has a length of 0 return instead of looping further. But its a comparision that would be made at every loop and could make this code a bit slower


Log in to reply
 

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