最长公共前缀

问题描述:

给k个字符串,求出他们的最长公共前缀(LCP)


解题思路:

自定义一个字典树结构,每个树节点有一个int型整数count,表示有多少个字符串经过该节点。除此之外,每个节点还有58个指针指向下一层节点,设计成58个指针,是因为‘a’与‘A’的ASCII编码相差58。

我们使用字典树表示所有字符串,构造字典树的时间复杂度为O(n*length),其中n为字符串个数,length为字符串的平均长度。

最后从根节点出发,遍历它的节点指针,然后比较它的下一个节点的成员变量count是否等于k,若不相等,说明它还有其他分支,则最长公共前缀到此为止。查找过程的时间复杂度为O(length),则总的时间复杂度为O(n*length)。


实现代码:

class Solution {
public:    
    /**
     * @param strs: A list of strings
     * @return: The longest common prefix
     */
     class trie{
         public:
         trie(){
             count  = 1;
             for(int i=0;i<58;i++)
                next[i] = NULL;
         };
         int count;
         trie* next[58];
     };
    string longestCommonPrefix(vector<string> &strs) {
        // write your code here
        string res;
        if(strs.size()==0)
        return res;
        
        
        trie* head = new trie();
        for(int i=0;i<strs.size();i++){
            trie* pre = head;
            for(int j=0;j<strs[i].size();j++){
               if(pre->next[strs[i][j]-'A']==NULL){
                   trie* tmp = new trie();
                   pre->next[strs[i][j]-'A'] = tmp;
               } else {
                   pre->next[strs[i][j]-'A']->count += 1;
               }
               pre = pre->next[strs[i][j]-'A'];
            }
            
        }
        while(head){
            int i=0;
            while(i<58 && head->next[i]==NULL)
                i++;
            if(i>=58 || head->next[i]->count!=strs.size())
                return res;
            res += 'A' +i;
            head = head->next[i];
        }
        
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值