[LeetCode] Longest Common Prefix

来源:互联网 发布:淘宝介入后卖家不举证 编辑:程序博客网 时间:2024/06/06 18:57
[Problem]
Write a function to find the longest common prefix string amongstan array of strings.

[Analysis]
Trie树的应用,不了解的可以先学一学Trie树,然后就可以秒杀这一题。

[Solution]

class Solution {
public:
// definition of TreeNode
struct TreeNode{
char ch; // character of the node
int count; // number of strings
vector<TreeNode*> children; // children of this node
TreeNode() : ch ('$'), count(1){}
TreeNode(char c) : ch(c){count = 1;}
};

// definition of Trie
struct Trie{
TreeNode* root; // root node of the Trie

// default constructor
Trie(){
root = new TreeNode('$');
}

// destructor
~Trie(){
destroy(root);
}

// destroy the Trie
void destroy(TreeNode* &node){
if(node != NULL){
for(int i = 0; i < node->children.size(); ++i){
destroy(node->children[i]);
}
delete node;
}
}

// insert a string into the Trie
void insert(TreeNode* &node, string str){
// null node or empty string
if(node == NULL || str == ""){
return;
}

// search the node
for(int i = 0; i < node->children.size(); ++i){
// found it
if(node->children[i]->ch == str[0]){
(node->children[i]->count)++;
insert(node->children[i], str.substr(1, str.length()-1));
return;
}
}

// a new node of the node
TreeNode* child = new TreeNode(str[0]);
node->children.push_back(child);
insert(child, str.substr(1, str.length()-1));
}

// get longest common prefix
vector<string> getLongest(TreeNode* &node, int count){
vector<string> res;

// null node
if(node == NULL || count <= 0){
return res;
}
if(node != root && node->count < count){
return res;
}

// search in its children
if(node->children.size() > 0){
// generate head
char tmp[2];
tmp[0] = node->ch;
tmp[1] = '\0';
string head(tmp);

for(int i = 0; i < node->children.size(); ++i){
vector<string> r = getLongest(node->children[i], count);
for(int j = 0; j < r.size(); ++j){
res.push_back(head + r[j]);
}
}

// there is no common prefix in it children, add itself
if(res.size() <= 0){
res.push_back(head);
}
}
else{
char tmp[2];
tmp[0] = node->ch;
tmp[1] = '\0';
res.push_back(tmp);
}

return res;
}

// print Trie
void print(TreeNode* &node){
// null node
if(node == NULL){
return;
}

// print itself
cout << "[" << node->ch << "," << node->count << "] ";

// print its children
for(int i = 0; i < node->children.size(); ++i){
print(node->children[i]);
}

// print a endl
if(node == root){
cout << endl;
}
}
};

// longest common prefix
string longestCommonPrefix(vector<string> &strs) {
// Start typing your C/C++ solution below
// DO NOT write int main() function

// empty input
if(strs.size() <= 0){
return "";
}

// create Trie
Trie* trie = new Trie();
for(int i = 0; i < strs.size(); ++i){
trie->insert(trie->root, strs[i]);
}

// print Trie
// trie->print(trie->root);

// get longest common prefix
string longest = "";
vector<string> res = trie->getLongest(trie->root, strs.size());
for(int i = 0; i < res.size(); ++i){
// NOTE: remove the root node '$'
if(res[i].length() - 1 > longest.length()){
longest = res[i].substr(1, res[i].length()-1);
}
}

// delete Trie
delete trie;

return longest;
}
};


 说明:版权所有,转载请注明出处。Coder007的博客