问题网址:https://leetcode.com/problems/longest-common-prefix/description/
问题描述:
编写一个函数来查找字符串数组中最长的公共前缀字符串。
LeetCode这道问题也是没有给出非常明确的输入输出,也是显得意义不明
下面给出解法
水平扫描法
首先,我们将描述找到由一组字符串LCP(S_1 \ Lots S_n)LCP(S 1 … S n)共享的最长前缀的简单方法。 我们将使用以下观察:
LCP(S_1 \ Lots S_n)= LCP(LCP(LCP(S_1,S_2),S_3),\ Lots S_n)LCP(S 1 … S n)= LCP(LCP(LCP ,S 2),S 3)… S n)
算法
为了使用这个想法,该算法遍历字符串[S_1 \ lots \ S_n] [S
1 … S n],在每次迭代中找出字符串LCP(S_1 \点S_i)LCP(S 1 … S i)的最长公共前缀当LCP(S_1 \ ldots S_i)LCP(S 1 … S i)是一个空字符串,算法结束。 否则,在n次迭代之后,算法返回LCP(S_1 \ ldots S_n)LCP(S 1 … S n)。
public String longestCommonPrefix(String[] strs) {
if (strs.length == 0) return "";
String prefix = strs[0];
for (int i = 1; i < strs.length; i++)
while (strs[i].indexOf(prefix) != 0) {
prefix = prefix.substring(0, prefix.length() - 1);
if (prefix.isEmpty()) return "";
}
return prefix;
}
垂直扫描法
想象一个非常短的字符串在数组的末尾。 上述方法仍然会进行SS比较。 优化这种情况的一种方法是进行垂直扫描。 在移动到下一列之前,我们在同一列(字符串的相同字符索引)上比较从上到下的字符。
public String longestCommonPrefix(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() || strs[j].charAt(i) != c)
return strs[0].substring(0, i);
}
}
return strs[0];
}
分治法
该算法的思想来自于LCP操作的关联性。我们注意到:LCP(S_1 \ lotsots S_n)= LCP(LCP(S_1 \ lotsots S_k),LCP(S_ {k + 1} \ lotsots S_n))LCP(S 1 … S n)= LCP (LCP(S 1 … S k),LCP(S k + 1 … S n)),其中LCP(S_1 \ lots S_n)LCP(S 1 … Sn)最长一组字符串中的通用前缀[S_1 \ ldots S_n] [S
1 … S n],1
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) return "";
return longestCommonPrefix(strs, 0 , strs.length - 1);
}
private String longestCommonPrefix(String[] strs, int l, int r) {
if (l == r) {
return strs[l];
}
else {
int mid = (l + r)/2;
String lcpLeft = longestCommonPrefix(strs, l , mid);
String lcpRight = longestCommonPrefix(strs, mid + 1,r);
return commonPrefix(lcpLeft, lcpRight);
}
}
String commonPrefix(String left,String right) {
int min = Math.min(left.length(), right.length());
for (int i = 0; i < min; i++) {
if ( left.charAt(i) != right.charAt(i) )
return left.substring(0, i);
}
return left.substring(0, min);
}
二分查找法
这个想法是应用二进制搜索方法来找到最大值为L的字符串,这是所有字符串的通用前缀。 算法搜索空间是间隔(0 \ ldots minLen)(0 … minLen),其中minLen是最小字符串长度和最大可能的公共前缀。 每次搜索空间被分成两个相等的部分,其中一个被丢弃,因为它确定它不包含解决方案。 有两种可能的情况:S [1 … mid]不是一个普通的字符串。 这意味着对于每个j> i S [1..j]不是一个普通的字符串,我们丢弃后半部分的搜索空间。 S [1 … mid]是普通的字符串。 这意味着对于每个i
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0)
return "";
int minLen = Integer.MAX_VALUE;
for (String str : strs)
minLen = Math.min(minLen, str.length());
int low = 1;
int high = minLen;
while (low <= high) {
int middle = (low + high) / 2;
if (isCommonPrefix(strs, middle))
low = middle + 1;
else
high = middle - 1;
}
return strs[0].substring(0, (low + high) / 2);
}
private boolean isCommonPrefix(String[] strs, int len){
String str1 = strs[0].substring(0,len);
for (int i = 1; i < strs.length; i++)
if (!strs[i].startsWith(str1))
return false;
return true;
}
进一步的思考与跟进
1367/5000
我们来看一个稍微不同的问题:
给定一组密钥S = [S_1,S_2 \ lots S_n] [S 1,S 2 … S n],找出串q和S中最长的公共前缀。这个LCP查询将被频繁地调用。
我们可以通过在Trie中存储一组密钥S来优化LCP查询。有关Trie的更多信息,请参阅本文实现trie(前缀trie)。在Trie中,从根开始递减的每个节点表示一些键的通用前缀。但是我们需要找到字符串q和所有关键字符串的最长公共前缀。这意味着我们必须从根中找到最深的路径,它满足以下条件:它是查询字符串的前缀q沿路径的每个节点必须只包含一个子元素。否则,找到的路径将不会成为所有字符串中的通用前缀。 *路径不包含标记为关键字结尾的节点。否则,路径不能是比自己短的密钥的前缀a。
算法
唯一的问题就是如何找到Trie最深的道路,满足上述要求。最有效的方法是从[S_1 \ ldots S_n] [S 1 … S n
]字符串。然后在Trie中查找查询字符串q的前缀。我们从根本上穿过Trie,直到不可能在Trie中继续这条路,因为上面的条件之一是不满足的。
public String longestCommonPrefix(String q, String[] strs) {
if (strs == null || strs.length == 0)
return "";
if (strs.length == 1)
return strs[0];
Trie trie = new Trie();
for (int i = 1; i < strs.length ; i++) {
trie.insert(strs[i]);
}
return trie.searchLongestPrefix(q);
}
class TrieNode {
// R links to node children
private TrieNode[] links;
private final int R = 26;
private boolean isEnd;
// number of children non null links
private int size;
public void put(char ch, TrieNode node) {
links[ch -'a'] = node;
size++;
}
public int getLinks() {
return size;
}
//assume methods containsKey, isEnd, get, put are implemented as it is described
//in https://leetcode.com/articles/implement-trie-prefix-tree/)
}
public class Trie {
private TrieNode root;
public Trie() {
root = new TrieNode();
}
//assume methods insert, search, searchPrefix are implemented as it is described
//in https://leetcode.com/articles/implement-trie-prefix-tree/)
private String searchLongestPrefix(String word) {
TrieNode node = root;
StringBuilder prefix = new StringBuilder();
for (int i = 0; i < word.length(); i++) {
char curLetter = word.charAt(i);
if (node.containsKey(curLetter) && (node.getLinks() == 1) && (!node.isEnd())) {
prefix.append(curLetter);
node = node.get(curLetter);
}
else
return prefix.toString();
}
return prefix.toString();
}
}