10ms java solution


  • 0
    I
    		int len = strs.length;
    		if (len == 0)
    			return "";
    		int min = strs[0].length();
    		String mins = strs[0];
    		char[][] c = new char[len][];
    		for (int i = 0; i < len; i++) {
    			c[i] = strs[i].toCharArray();
    		}
    		for (int i = 1; i < strs.length; i++) {
    			if (strs[i].length() < min) {
    				min = strs[i].length();
    				mins = strs[i];
    			}
    		}
    		if (min == 0)
    			return new String();
    		for (int i = 0; i < min; i++) {
    			for (int j = 0; j < len; j++) {
    				if (c[0][i] != c[j][i])
    					return i == 0 ? "" : new String(
    							strs[0].substring(0, i));
    			}
    		}
    		return mins;
    	}

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.