Longest Common Prefix
Input: ["flower","flow","flight"]
Output: "fl"Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.Solution
class Solution {
public String longestCommonPrefix(String[] strs) {
int min = Integer.MAX_VALUE;
for (String s : strs) {
min = Math.min(min, s.length());
}
if (min == 0) return "";
Trie root = new Trie(null);
for (String s : strs) {
Trie current = root;
for (int i = 0; i < min; i++) {
Character c = s.charAt(i);
if (current.values.containsKey(c)) {
current = current.values.get(c);
} else {
Trie t = new Trie(c);
current.values.put(c, t);
current = t;
}
}
}
if (root.values.size() != 1) {
return "";
}
return buildCommonPrefix(root);
}
private String buildCommonPrefix(Trie node) {
if (node.values.size() != 1) { // it must be 1, otherwise they split and it is not common prefix
return String.valueOf(node.value);
}
String result = "";
if (node.value != null) {
result += node.value;
}
result += buildCommonPrefix(node.values.entrySet().iterator().next().getValue());
return result;
}
}
class Trie {
Character value;
Map<Character, Trie> values = new HashMap<>();
Trie(Character c) { value = c; }
}Last updated