-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy path14-longest-common-prefix.cpp
More file actions
131 lines (106 loc) · 4.7 KB
/
14-longest-common-prefix.cpp
File metadata and controls
131 lines (106 loc) · 4.7 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
Problem 14: Longest Common Prefix
https://leetcode.com/problems/longest-common-prefix/
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string "".
Example 1:
Input: strs = ["flower","flow","flight"]
Output: "fl"
Example 2:
Input: strs = ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.
Constraints:
- 1 <= strs.length <= 200
- 0 <= strs[i].length <= 200
- strs[i] consists of only lowercase English letters.
*/
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
class Solution {
public:
// Approach 1: Horizontal scanning
std::string longestCommonPrefix(std::vector<std::string>& strs) {
if (strs.empty()) {
return "";
}
std::string prefix = strs[0];
for (int i = 1; i < strs.size(); ++i) {
while (strs[i].find(prefix) != 0) {
prefix = prefix.substr(0, prefix.length() - 1);
if (prefix.empty()) {
return "";
}
}
}
return prefix;
}
// Approach 2: Vertical scanning
std::string longestCommonPrefixVertical(std::vector<std::string>& strs) {
if (strs.empty()) {
return "";
}
for (int i = 0; i < strs[0].length(); ++i) {
char c = strs[0][i];
for (int j = 1; j < strs.size(); ++j) {
if (i == strs[j].length() || strs[j][i] != c) {
return strs[0].substr(0, i);
}
}
}
return strs[0];
}
// Approach 3: Divide and Conquer
std::string longestCommonPrefixDivideConquer(std::vector<std::string>& strs) {
if (strs.empty()) {
return "";
}
return longestCommonPrefixRecursive(strs, 0, strs.size() - 1);
}
private:
std::string longestCommonPrefixRecursive(const std::vector<std::string>& strs, int left, int right) {
if (left == right) {
return strs[left];
}
int mid = (left + right) / 2;
std::string lcpLeft = longestCommonPrefixRecursive(strs, left, mid);
std::string lcpRight = longestCommonPrefixRecursive(strs, mid + 1, right);
return commonPrefix(lcpLeft, lcpRight);
}
std::string commonPrefix(const std::string& str1, const std::string& str2) {
int minLength = std::min(str1.length(), str2.length());
for (int i = 0; i < minLength; ++i) {
if (str1[i] != str2[i]) {
return str1.substr(0, i);
}
}
return str1.substr(0, minLength);
}
};
int main() {
Solution sol;
std::cout << "Testing Longest Common Prefix (Horizontal Scanning):\n";
std::vector<std::string> strs1 = {"flower", "flow", "flight"};
std::cout << "Input: [\"flower\",\"flow\",\"flight\"] -> Output: " << sol.longestCommonPrefix(strs1) << " (Expected: fl)\n";
std::vector<std::string> strs2 = {"dog", "racecar", "car"};
std::cout << "Input: [\"dog\",\"racecar\",\"car\"] -> Output: " << sol.longestCommonPrefix(strs2) << " (Expected: )\n";
std::vector<std::string> strs3 = {"apple", "apricot", "april"};
std::cout << "Input: [\"apple\",\"apricot\",\"april\"] -> Output: " << sol.longestCommonPrefix(strs3) << " (Expected: ap)\n";
std::cout << "\nTesting Longest Common Prefix (Vertical Scanning):\n";
std::vector<std::string> strs4 = {"flower", "flow", "flight"};
std::cout << "Input: [\"flower\",\"flow\",\"flight\"] -> Output: " << sol.longestCommonPrefixVertical(strs4) << " (Expected: fl)\n";
std::vector<std::string> strs5 = {"dog", "racecar", "car"};
std::cout << "Input: [\"dog\",\"racecar\",\"car\"] -> Output: " << sol.longestCommonPrefixVertical(strs5) << " (Expected: )\n";
std::vector<std::string> strs6 = {"apple", "apricot", "april"};
std::cout << "Input: [\"apple\",\"apricot\",\"april\"] -> Output: " << sol.longestCommonPrefixVertical(strs6) << " (Expected: ap)\n";
std::cout << "\nTesting Longest Common Prefix (Divide and Conquer):\n";
std::vector<std::string> strs7 = {"flower", "flow", "flight"};
std::cout << "Input: [\"flower\",\"flow\",\"flight\"] -> Output: " << sol.longestCommonPrefixDivideConquer(strs7) << " (Expected: fl)\n";
std::vector<std::string> strs8 = {"dog", "racecar", "car"};
std::cout << "Input: [\"dog\",\"racecar\",\"car\"] -> Output: " << sol.longestCommonPrefixDivideConquer(strs8) << " (Expected: )\n";
std::vector<std::string> strs9 = {"apple", "apricot", "april"};
std::cout << "Input: [\"apple\",\"apricot\",\"april\"] -> Output: " << sol.longestCommonPrefixDivideConquer(strs9) << " (Expected: ap)\n";
return 0;
}