1public String longestCommonPrefix(String[] strs) {
2 if (strs == null || strs.length == 0) {
3 return "";
4 }
5
6 // Use first string as reference
7 for (int i = 0; i < strs[0].length(); i++) {
8 char c = strs[0].charAt(i);
9
10 // Check if all strings have this character at position i
11 for (int j = 1; j < strs.length; j++) {
12 if (i >= strs[j].length() || strs[j].charAt(i) != c) {
13 return strs[0].substring(0, i);
14 }
15 }
16 }
17
18 return strs[0];
19}