{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "<h1>14. Longest Common Prefix</h1>\n", "<hr>\n", "\n", "<!--Copy Paste Leetcode statement between-->\n", "<p>Write a function to find the longest common prefix string amongst an array of strings.</p>\n", "\n", "<p>If there is no common prefix, return an empty string <code>\"\"</code>.</p>\n", "\n", "<p> </p>\n", "<p><strong>Example 1:</strong></p>\n", "\n", "<pre><strong>Input:</strong> strs = [\"flower\",\"flow\",\"flight\"]\n", "<strong>Output:</strong> \"fl\"\n", "</pre>\n", "\n", "<p><strong>Example 2:</strong></p>\n", "\n", "<pre><strong>Input:</strong> strs = [\"dog\",\"racecar\",\"car\"]\n", "<strong>Output:</strong> \"\"\n", "<strong>Explanation:</strong> There is no common prefix among the input strings.\n", "</pre>\n", "\n", "<p> </p>\n", "<p><strong>Constraints:</strong></p>\n", "\n", "<ul>\n", "\t<li><code>0 <= strs.length <= 200</code></li>\n", "\t<li><code>0 <= strs[i].length <= 200</code></li>\n", "\t<li><code>strs[i]</code> consists of only lower-case English letters.</li>\n", "</ul>\n", "<!--Copy Paste Leetcode statement between-->\n", "\n", "<p> </p>\n", "<a href=\"https://leetcode.com/problems/longest-common-prefix/\">Source</a> \n", "<hr>\n", "\n", "<h4>Code</h4>" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def longest_common_prefix(strs):\n", " \"\"\"Vertical scanning.\"\"\"\n", " if not strs:\n", " return \"\"\n", " prefix = min(strs, key=len)\n", " for i, char in enumerate(prefix):\n", " for word in strs:\n", " if word[i] != char:\n", " return prefix[:i]\n", " return prefix" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from functools import reduce\n", "\n", "def longest_common_prefix(strs): \n", " \"\"\"Horizontal scanning using reduce\n", " Time complexity : O(S) (s: m*n - sum of all letters, in worst case all words have same lenghts)\n", " Space complexity : O(1)\n", " \"\"\"\n", " def merge(str1, str2):\n", " i = 0\n", " while i < len(str1) and i < len(str2) and str1[i] == str2[i]:\n", " i += 1\n", " return str1[:i] \n", " \n", " if not strs:\n", " return \"\"\n", " return reduce(merge, strs) # merge the longest common prefix in first 2 words, then reduce with others" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def longest_common_prefix(strs):\n", " \"\"\"Using a set of i-th letter of each word.\"\"\"\n", " prefix = ''\n", " for column_slice in zip(*strs): # tuples of all i-th letter of each word (up to len() of smallest word)\n", " if len(set(column_slice)) == 1: # if the set only contains 1 element = same letter\n", " prefix += column_slice[0]\n", " else:\n", " break\n", " return prefix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<h4>Check</h4>" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'fl'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "strs = [\"flower\", \"flow\", \"flight\"]\n", "\n", "longest_common_prefix(strs)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "''" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "strs = [\"dog\", \"racecar\", \"car\"]\n", "\n", "longest_common_prefix(strs)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'c'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "strs = [\"cir\", \"car\"]\n", "\n", "longest_common_prefix(strs)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'cat'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "strs = [\"cat\"]\n", "\n", "longest_common_prefix(strs)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "''" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "strs = []\n", "\n", "longest_common_prefix(strs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<hr>\n", "<h4>Follow up:</h4>\n", "<p>Could you solve it using the following implementation:</p>\n", "<ul>\n", "\t<li>Divide and Conquer</li>\n", "\t<li>Binary Search</li>\n", "\t<li>Trie</li>\n", "</ul>\n", "\n", "<h4>Code</h4>" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "def longest_common_prefix(strs):\n", " \"\"\"Using Divide and Conquer\"\"\"\n", " pass" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "def longest_common_prefix(strs):\n", " \"\"\"Using Binary Search\"\"\"\n", " pass" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "def longest_common_prefix(strs):\n", " \"\"\"Using Trie\"\"\"\n", " pass" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.2" } }, "nbformat": 4, "nbformat_minor": 1 }