-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongestCommonPrefix.h
58 lines (48 loc) · 1.67 KB
/
LongestCommonPrefix.h
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
/*
Author: naiyong, aonaiyong@gmail.com
Date: Nov 18, 2014
Problem: Longest Common Prefix
Difficulty: 2
Source: https://oj.leetcode.com/problems/longest-common-prefix/
Notes:
Write a function to find the longest common prefix string amongst an array of strings.
Solution: Simply compare corresponding positions of each string, and terminate if a mismatch is encountered.
*/
#ifndef LONGESTCOMMONPREFIX_H_
#define LONGESTCOMMONPREFIX_H_
#include <vector>
using std::vector;
#include <string>
using std::string;
#include <algorithm>
using std::min;
class Solution {
public:
string longestCommonPrefix(vector<string> &strs) {
return longestCommonPrefix_2(strs);
}
string longestCommonPrefix_1(const vector<string> &strs) {
if (strs.empty()) return {};
int m = strs.size(), n = strs[0].size();
for (int j = 0; j < n; ++j) {
for (int i = 1; i < m; ++i)
if (j >= strs[i].size() || strs[i][j] != strs[0][j])
return strs[0].substr(0, j);
}
return strs[0];
}
string longestCommonPrefix_2(const vector<string> &strs) {
if (strs.empty()) return {};
size_t m = strs.size(), n = strs[0].size();
// Compute the minimum string size first
for (int i = 1; i < m; ++i)
n = min(n, strs[i].size());
for (int j = 0; j < n; ++j) {
for (int i = 1; i < m; ++i)
if (strs[i][j] != strs[0][j])
return strs[0].substr(0, j);
}
return strs[0].substr(0, n);
}
};
#endif /* LONGESTCOMMONPREFIX_H_ */