Here you can find the source of longestCommonPrefix1(String[] strs)
Parameter | Description |
---|---|
strs | a parameter |
public static String longestCommonPrefix1(String[] strs)
//package com.java2s; public class Main { /**//w ww . j ava 2s . com * Horizontal scanning * * @param strs * @return */ public static String longestCommonPrefix1(String[] strs) { if (strs.length == 0) { return ""; } String prefix = strs[0]; for (int i = 0; i < strs.length; i++) { while (strs[i].indexOf(prefix) != 0) { prefix = prefix.substring(0, prefix.length() - 1); if (prefix.isEmpty()) { return ""; } } } return prefix; } }