We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 3087fbb commit 426f659Copy full SHA for 426f659
21 - Trie Data Structure Problems/03 - Longest Common Prefix/Best Approach.cpp
@@ -0,0 +1,23 @@
1
+class Solution {
2
+public:
3
+ string longestCommonPrefix(vector<string>& strs) {
4
+
5
+ string ans = "";
6
+ for(int i = 0; i < strs[0].length(); i++){
7
+ char ch = strs[0][i];
8
9
+ bool match = true;
10
+ for(int j = 1; j < strs.size(); j++){
11
+ if(strs[j].size() < i || ch != strs[j][i]){
12
+ match = false;
13
+ break;
14
+ }
15
16
17
+ if(match == false) break;
18
+ else ans.push_back(ch);
19
20
21
+ return ans;
22
23
+};
0 commit comments