Here you can find the source of longestCommonPrefix(String[] strs)
public static String longestCommonPrefix(String[] strs)
//package com.java2s; //License from project: Open Source License public class Main { public static String longestCommonPrefix(String[] strs) { String str = ""; if (strs.length == 0) return str; if (strs.length == 1) return strs[0]; for (int i = 1; i < strs.length; i++) { if (i == 1) { int n = strs[0].length() > strs[1].length() ? strs[1] .length() : strs[0].length(); for (int j = 0; j < n; j++) { if (strs[0].charAt(j) == strs[1].charAt(j)) str = str + strs[0].charAt(j); else break; }/*from w ww . ja v a 2 s. c o m*/ } else { while (!strs[i].startsWith(str)) str = str.substring(0, str.length() - 1); } } return str; } }