-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongest_common_prefix.cpp
More file actions
67 lines (61 loc) · 1.6 KB
/
longest_common_prefix.cpp
File metadata and controls
67 lines (61 loc) · 1.6 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* =====================================================================================
*
* Filename: longest_common_prefix.cpp
*
* Description: 14. Longest Common Prefix: Write a function to find the longest
* common prefix string amongst an array of strings.
*
* Version: 1.0
* Created: 06/29/18 10:48:56
* Revision: none
* Compiler: gcc
*
* Author: Zhu Xianfeng (), xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <stdio.h>
#include <string>
#include <vector>
// Vertical scanning
class Solution
{
public:
std::string longestCommonPrefix(const std::vector<std::string>& strs)
{
if (strs.size() == 0)
{
return "";
}
for (int i = 0; i < strs[0].size(); i++)
{
char chr = strs[0][i];
for (int j = 1; j < strs.size(); j++)
{
if (i == strs[j].size() || strs[j][i] != chr)
{
// Stop scanning
return strs[0].substr(0, i);
}
}
}
return strs[0];
}
};
int main(int argc, char* argv[])
{
std::vector<std::string> strs = {"flower", "flow", "flight"};
if (argc > 2)
{
strs.clear();
for (int i = 1; i < argc; i++)
{
strs.push_back(argv[i]);
}
}
std::string prefix = Solution().longestCommonPrefix(strs);
printf("longest common prefix: %s\n", prefix.c_str());
return 0;
}