Java code 71%


  • 0
    X

    public class Solution {
    public String longestCommonPrefix(String[] strs) {

        if (strs.length == 0) {
            return "";
        } 
        
        int strLen = strs[0].length();
        StringBuilder prefix = new StringBuilder();
        
        for (int index = 0; index < strLen; index++) {
            char x = strs[0].charAt(index);
            
            for(int i=1; i< strs.length; i++) {
                if (index >= strs[i].length() || strs[i].charAt(index) != x) {
                    return prefix.toString();
                } 
            }
                 
            prefix.append(x);
        }
        
        return prefix.toString();
    }
    

    }


Log in to reply
 

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