# [14\. Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/) :::spoiler Hint ```cpp= class Solution { public: string longestCommonPrefix(vector<string>& strs) { // Sort the vector of strings lexicographically // Find the minimum length between the first and the last string in the sorted vector // Initialize an index to track the position of the common prefix // Compare characters of the first and last strings in the sorted vector // Increment the index if the characters match // Return the substring from the start to the position where characters matched } }; ``` ::: :::spoiler Solution ```cpp= class Solution { public: string longestCommonPrefix(vector<string>& strs) { sort(strs.begin(), strs.end()); int minLength = min(strs.front().size(), strs.back().size()); int i = 0; while(i < minLength && strs.front()[i] == strs.back()[i]) { ++i; } return strs.front().substr(0, i); } }; ``` - T: $O(N \log N)$ - S: $O(1)$ :::