最长公共前缀 II

http://www.lintcode.com/zh-cn/problem/the-longest-common-prefix-ii/

import java.util.List;

public class Solution {
    /**
     * @param dic:    the n strings
     * @param target: the target string
     * @return: The ans
     */
    public int the_longest_common_prefix(List dic, String target) {
        // write your code here
        if (dic == null || dic.size() == 0) {
            return 0;
        }
        if (target == null || target.length() == 0) {
            return 0;
        }
        for (int i = 0; i < target.length(); i++) {
            boolean b = false;
            char c = target.charAt(i);
            for (int j = 0; j < dic.size(); j++) {
                String s = dic.get(j);
                if (s == null || s.length() - 1 < i) {
                    dic.remove(j);
                    j--;
                } else if (s.charAt(i) == c) {
                    b = true;
                } else {
                    dic.remove(j);
                    j--;
                }
            }
            if (!b) {
                return i;
            }
        }
        return target.length();
    }
}

你可能感兴趣的:(最长公共前缀 II)