{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

14. Longest Common Prefix

\n", "
\n", "\n", "\n", "

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

\n", "\n", "

If there is no common prefix, return an empty string \"\".

\n", "\n", "

 

\n", "

Example 1:

\n", "\n", "
Input: strs = [\"flower\",\"flow\",\"flight\"]\n",
    "Output: \"fl\"\n",
    "
\n", "\n", "

Example 2:

\n", "\n", "
Input: strs = [\"dog\",\"racecar\",\"car\"]\n",
    "Output: \"\"\n",
    "Explanation: There is no common prefix among the input strings.\n",
    "
\n", "\n", "

 

\n", "

Constraints:

\n", "\n", "\n", "\n", "\n", "

 

\n", "Source \n", "
\n", "\n", "

Code

" ] }, { "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": [ "

Check

" ] }, { "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": [ "
\n", "

Follow up:

\n", "

Could you solve it using the following implementation:

\n", "\n", "\n", "

Code

" ] }, { "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 }