14. Longest Common Prefix
1 class Solution { 2 public String longestCommonPrefix(String[] strs) { 3 if(strs.length == 0) return ""; 4 StringBuilder sb = new StringBuilder(""); 5 for(int i = 0; i < strs[0].length(); i++) { 6 for(int j = 1; j < strs.length; j++) { 7 if(i < strs[j].length() && strs[0].charAt(i) == strs[j].charAt(i)) { 8 continue; 9 }else { 10 return sb.toString(); 11 } 12 } 13 sb.append(strs[0].charAt(i)); 14 } 15 return sb.toString(); 16 } 17 }