Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions leetcode/cpp/string/14.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* 14. 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: str = [ "flower", "flow", "flight" ]
* output: "fl"
*
*
@author of the code is vi (yeah that's the nickname
*/

class Solution {
constexpr static size_t THE_PREFIX_START_{0};

public:
constexpr inline std::string longestCommonPrefix(const std::vector<std::string>& strs) noexcept {
std::string common_prefix{strs[0]};

// iterating through every string in strs
// e stands for element
for(const auto& e : strs) {
// if we didn't find the prefix, we'll change the prefix until we found it
while(e.find(common_prefix) != THE_PREFIX_START_) {
common_prefix = common_prefix.substr(0, common_prefix.size()-1);
// if common_prefix is an empty string, than it's over
if(common_prefix.empty()) {
return "";
}
}
}

return common_prefix;
}
};