Here you can find the source of longestCommonPrefix(String[] strs)
static String longestCommonPrefix(String[] strs)
//package com.java2s; //License from project: Open Source License public class Main { /**//from ww w .j a v a 2 s . c o m * Simple approach to compare first, second, ... elements of every element in array with first element. * if any string with not equal character found, return prefix. */ static String longestCommonPrefix(String[] strs) { if (strs.length == 0) return ""; if (strs.length == 1) return strs[0]; int len = strs[0].length(); int i = 0; for (; i < len; i++) { for (int j = 1; j < strs.length; j++) { if (i == strs[j].length() || strs[0].charAt(i) != strs[j].charAt(i)) return strs[0].substring(0, i); } } return strs[0].substring(0, i); } }