-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem14.cpp
More file actions
32 lines (32 loc) · 782 Bytes
/
problem14.cpp
File metadata and controls
32 lines (32 loc) · 782 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
class Solution
{
public:
string longestCommonPrefix(vector<string> &strs)
{
string prefix = "";
if (strs.size() == 1 || strs[0] == "")
{
return strs[0];
}
int counter = 0;
bool eq = false;
while (!eq)
{
std::cout << counter;
char temp = strs[0].at(counter);
for (int i = 0; i < strs.size(); i++)
{
if (counter >= strs[i].size() || strs[i].at(counter) != temp)
eq = true;
}
if (eq)
break;
prefix += temp;
if (counter >= strs[0].size() - 1)
break;
counter++;
eq = false;
}
return prefix;
}
};