-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongest_prefix.cpp
More file actions
41 lines (31 loc) · 804 Bytes
/
longest_prefix.cpp
File metadata and controls
41 lines (31 loc) · 804 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <vector>
using namespace std;
int longest_prefix(string input[], int length){ //lenght -> input's size
int i, j;
int min;
int count = 0;
vector<char> temp[length];
for(i = 0; i < length; i++){
copy(input[i].begin(), input[i].end(), back_inserter(temp[i]));
}
for(i = 0; i < length; i++){
min = temp[0].size();
if(temp[i].size() < min){
min = temp[i].size();
}
}
for(i = 0; i < min; i++){
int index_count = 0;
char equal_temp = temp[0][i];
for(j = 1; j < length; j++){
if(equal_temp == temp[j][i]){
index_count++;
}
}
if(index_count == length-1){
count++;
}
}
return count;
}