Here you can find the source of longestCommonPrefix2(String[] strs)
Parameter | Description |
---|---|
strs | a parameter |
public static String longestCommonPrefix2(String[] strs)
//package com.java2s; public class Main { /**//from ww w . j a v a 2s.c om * Vertical scanning * * @param strs * @return */ public static String longestCommonPrefix2(String[] strs) { if (strs == null || strs.length == 0) { return ""; } for (int i = 0; i < strs[0].length(); i++) { char c = strs[0].charAt(i); for (int j = 1; j < strs.length; j++) { if (i == strs[j].length() || c != strs[j].charAt(i)) { return strs[0].substring(0, i); } } } return strs[0]; } }