My simple Java Solution


  • 0
    G
    public class Solution {
    public String longestCommonPrefix(String[] strs) {
        String res = "";
        int i = 0;
        try{
            while(true){
                char now = strs[0].charAt(i);
                for(int j = 0; j<strs.length;j++){
                    if(strs[j].charAt(i) != now) return res;
                    
                }
                res+=now;
                i++;
            }
        }catch(Exception e){
            return res;
        }
    }
    

    }


Log in to reply
 

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