{"id":33784,"date":"2024-11-01T09:20:20","date_gmt":"2024-11-01T09:20:20","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33784"},"modified":"2024-11-01T12:27:06","modified_gmt":"2024-11-01T12:27:06","slug":"%ed%8c%8c%ec%9d%b4%ec%8d%ac-%ec%bd%94%eb%94%a9%ed%85%8c%ec%8a%a4%ed%8a%b8-%ea%b0%95%ec%a2%8c-%ea%b0%80%ec%9e%a5-%ea%b8%b4-%ea%b3%b5%ed%86%b5-%eb%b6%80%eb%b6%84-%ec%88%98%ec%97%b4-%ec%b0%be%ea%b8%b0","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33784\/","title":{"rendered":"Python coding test course, finding the longest common subsequence"},"content":{"rendered":"<p><body><\/p>\n<h2>Problem Description<\/h2>\n<p>\n        Given two strings, this is a problem of finding the Longest Common Subsequence (LCS) between them. A common subsequence refers to characters that appear in both strings while maintaining their order. The goal is to find the maximum length of such a subsequence.\n    <\/p>\n<h2>Example<\/h2>\n<h3>Input<\/h3>\n<p>\n        String A: <strong>ABCBDAB<\/strong><br \/>\n        String B: <strong>BDCAB<\/strong>\n<\/p>\n<h3>Output<\/h3>\n<p>\n        Longest Common Subsequence: <strong>BDAB<\/strong>\n<\/p>\n<h2>Approach to the Problem<\/h2>\n<p>\n        To solve the LCS problem, we can use Dynamic Programming. Dynamic Programming is a technique that solves problems by storing already computed results and reusing them. To find the LCS, we store the length of the subsequence based on the comparison results of each character in the two strings.\n    <\/p>\n<h2>Finding LCS using Dynamic Programming<\/h2>\n<h3>Step 1: Initialize the DP Table<\/h3>\n<p>\n        Let the lengths of the two strings be <code>m<\/code> and <code>n<\/code>, we create and initialize a 2D DP array of size <code>(m + 1) x (n + 1)<\/code>. Each element of the DP array stores the length of the common subsequence found in the two strings.\n    <\/p>\n<h3>Step 2: Fill the DP Table<\/h3>\n<p>\n        We fill in the DP table while comparing each character of the two strings. If the characters match, the LCS length including that character is <code>DP[i-1][j-1] + 1<\/code>. If they do not match, we use <code>DP[i][j] = max(DP[i-1][j], DP[i][j-1])<\/code> to record the length of the LCS found so far.\n    <\/p>\n<h3>Step 3: Extract LCS Length and String<\/h3>\n<p>\n        After filling the DP table, check <code>DP[m][n]<\/code> to get the length of the longest common subsequence, and extract the actual common subsequence using this length. The extraction process is carried out by tracing back through the table to find common characters.\n    <\/p>\n<h2> Algorithm Code <\/h2>\n<pre>\n    <code>\ndef lcs(A, B):\n    # Step 1: Initialize the DP array\n    m = len(A)\n    n = len(B)\n    dp = [[0] * (n + 1) for _ in range(m + 1)]\n\n    # Step 2: Fill the DP array\n    for i in range(1, m + 1):\n        for j in range(1, n + 1):\n            if A[i - 1] == B[j - 1]:\n                dp[i][j] = dp[i - 1][j - 1] + 1\n            else:\n                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])\n\n    # Step 3: Get LCS length\n    lcs_length = dp[m][n]\n\n    # Extract LCS string\n    lcs_str = []\n    i, j = m, n\n    while i > 0 and j > 0:\n        if A[i - 1] == B[j - 1]:\n            lcs_str.append(A[i - 1])\n            i -= 1\n            j -= 1\n        elif dp[i - 1][j] >= dp[i][j - 1]:\n            i -= 1\n        else:\n            j -= 1\n\n    lcs_str.reverse()  # Reverse the extracted string to restore original order\n    return lcs_length, ''.join(lcs_str)\n\n# Test code\nA = \"ABCBDAB\"\nB = \"BDCAB\"\nlength, lcs_string = lcs(A, B)\nprint(\"LCS length:\", length)\nprint(\"LCS string:\", lcs_string)\n    <\/code>\n    <\/pre>\n<h2>Complexity Analysis<\/h2>\n<p>\n        The time complexity of this algorithm is O(m * n). Since each character needs to be compared, the result is a product of the lengths of the two strings. The space complexity is also O(m * n), determined by the size of the created DP table. However, there are ways to reduce the size of the DP table. For example, since only the previous row&#8217;s data is needed, it can be implemented using two 1D arrays.\n    <\/p>\n<h3>Optimized Code Example<\/h3>\n<pre>\n    <code>\ndef optimized_lcs(A, B):\n    m = len(A)\n    n = len(B)\n    dp = [0] * (n + 1)\n\n    for i in range(1, m + 1):\n        current = 0  # Value at the current position\n        for j in range(1, n + 1):\n            temp = dp[j]\n            if A[i - 1] == B[j - 1]:\n                dp[j] = current + 1\n            else:\n                dp[j] = max(dp[j], dp[j - 1])\n            current = temp  # Store the value from the previous row\n\n    return dp[n]\n\n# Optimized test\nlength = optimized_lcs(A, B)\nprint(\"LCS length:\", length)\n    <\/code>\n    <\/pre>\n<h2>Conclusion<\/h2>\n<p>\n        In this lecture, we dealt with the problem of finding the Longest Common Subsequence (LCS). This problem is a great example of how to easily utilize string processing and dynamic programming concepts. The LCS problem is widely used in various fields, especially in gene analysis and finding identical sequences, so understanding and implementing the algorithm is very useful for coding tests and practical applications.\n    <\/p>\n<h2>References<\/h2>\n<ul>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Longest_common_subsequence_problem\">Longest Common Subsequence &#8211; Wikipedia<\/a><\/li>\n<li><a href=\"https:\/\/www.geeksforgeeks.org\/longest-common-subsequence-dp-4\/\">GeeksforGeeks: Longest Common Subsequence<\/a><\/li>\n<\/ul>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Description Given two strings, this is a problem of finding the Longest Common Subsequence (LCS) between them. A common subsequence refers to characters that appear in both strings while maintaining their order. The goal is to find the maximum length of such a subsequence. Example Input String A: ABCBDAB String B: BDCAB Output Longest &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33784\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Python coding test course, finding the longest common subsequence&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[145],"tags":[],"class_list":["post-33784","post","type-post","status-publish","format-standard","hentry","category-python-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python coding test course, finding the longest common subsequence - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/33784\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python coding test course, finding the longest common subsequence - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Problem Description Given two strings, this is a problem of finding the Longest Common Subsequence (LCS) between them. A common subsequence refers to characters that appear in both strings while maintaining their order. The goal is to find the maximum length of such a subsequence. Example Input String A: ABCBDAB String B: BDCAB Output Longest &hellip; \ub354 \ubcf4\uae30 &quot;Python coding test course, finding the longest common subsequence&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33784\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:20:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T12:27:06+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/33784\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33784\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Python coding test course, finding the longest common subsequence\",\"datePublished\":\"2024-11-01T09:20:20+00:00\",\"dateModified\":\"2024-11-01T12:27:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33784\/\"},\"wordCount\":419,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Python Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33784\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33784\/\",\"name\":\"Python coding test course, finding the longest common subsequence - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:20:20+00:00\",\"dateModified\":\"2024-11-01T12:27:06+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33784\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33784\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33784\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python coding test course, finding the longest common subsequence\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python coding test course, finding the longest common subsequence - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/33784\/","og_locale":"ko_KR","og_type":"article","og_title":"Python coding test course, finding the longest common subsequence - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Problem Description Given two strings, this is a problem of finding the Longest Common Subsequence (LCS) between them. A common subsequence refers to characters that appear in both strings while maintaining their order. The goal is to find the maximum length of such a subsequence. Example Input String A: ABCBDAB String B: BDCAB Output Longest &hellip; \ub354 \ubcf4\uae30 \"Python coding test course, finding the longest common subsequence\"","og_url":"https:\/\/atmokpo.com\/w\/33784\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:20:20+00:00","article_modified_time":"2024-11-01T12:27:06+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/33784\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33784\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Python coding test course, finding the longest common subsequence","datePublished":"2024-11-01T09:20:20+00:00","dateModified":"2024-11-01T12:27:06+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33784\/"},"wordCount":419,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Python Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33784\/","url":"https:\/\/atmokpo.com\/w\/33784\/","name":"Python coding test course, finding the longest common subsequence - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:20:20+00:00","dateModified":"2024-11-01T12:27:06+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33784\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33784\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33784\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Python coding test course, finding the longest common subsequence"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33784","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=33784"}],"version-history":[{"count":2,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33784\/revisions"}],"predecessor-version":[{"id":38050,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33784\/revisions\/38050"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33784"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33784"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33784"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}