Skip to content

Instantly share code, notes, and snippets.

@aaani
Last active December 26, 2015 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaani/7134968 to your computer and use it in GitHub Desktop.
Save aaani/7134968 to your computer and use it in GitHub Desktop.
int LCPLength(vector<string> input){
//Find the length of shortest string in the input set
int minLen = INT32_MAX;
for(int i=0;i<input.size();i++){
if(input[i].length()<minLen) minLen=(int)input[i].length();
}
bool done = false;
int lcpLen=0;
for(int j=0;j<minLen;j++){
for(int i=0;i<input.size() && !done;i++){
//Whenever there is a mismatch, set done flag to terminate the function
if(input[i][j] != input[0][j]) done=true;
}
if(done) break;
lcpLen++;
}
return lcpLen;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment