<p>Write a function to find the longest common prefix string amongst an array of strings.</p>

<p>If there is no common prefix, return an empty string <code>&quot;&quot;</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> strs = [&quot;flower&quot;,&quot;flow&quot;,&quot;flight&quot;]
<strong>Output:</strong> &quot;fl&quot;
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> strs = [&quot;dog&quot;,&quot;racecar&quot;,&quot;car&quot;]
<strong>Output:</strong> &quot;&quot;
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= strs.length &lt;= 200</code></li>
	<li><code>0 &lt;= strs[i].length &lt;= 200</code></li>
	<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>

class Solution {
public:
    // Helper function to find the common prefix between two strings
    string commonPrefix(const string& str1, const string& str2) {
        int minLength = min(str1.length(), str2.length());
        for (int i = 0; i < minLength; ++i) {
            if (str1[i] != str2[i]) {
                return str1.substr(0, i);  // Return the common prefix found so far
            }
        }
        return str1.substr(0, minLength);  // Return the entire shorter string if all characters match
    }

    // Main function to find the longest common prefix among an array of strings
    string longestCommonPrefix(vector<string>& strs) {
        if (strs.empty()) return "";  // If the input vector is empty, return an empty string
        return longestCommonPrefixHelper(strs, 0, strs.size() - 1);  // Call the helper function
    }

    // Helper function for divide and conquer
    string longestCommonPrefixHelper(vector<string>& strs, int left, int right) {
        if (left == right) {
            return strs[left];  // Base case: only one string
        }
        int mid = left + (right - left) / 2;  // Find the midpoint
        string leftPrefix = longestCommonPrefixHelper(strs, left, mid);  // Recursively find prefix in the left half
        string rightPrefix = longestCommonPrefixHelper(strs, mid + 1, right);  // Recursively find prefix in the right half
        return commonPrefix(leftPrefix, rightPrefix);  // Combine the results
    }
};
13300