Clean JAVA Code


  • 0
    A
    public String longestCommonPrefix(String[] strs) {
    	if (strs == null || strs.length == 0)
    		return "";
    	int index = 0;
    	while (isMatch(strs, index))
    		index++;
    	return strs[0].substring(0, index);
    }
    
    public boolean isMatch(String[] strs, int index) {
    	if (index >= strs[0].length())
    		return false;
    	char c = strs[0].charAt(index);
    	for (String str : strs)
    		if (index >= str.length() || str.charAt(index) != c)
    			return false;
    	return true;
    }

Log in to reply
 

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