{"id":35148,"date":"2024-11-01T09:36:07","date_gmt":"2024-11-01T09:36:07","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=35148"},"modified":"2024-11-01T11:44:49","modified_gmt":"2024-11-01T11:44:49","slug":"kotlin-coding-test-course-longest-common-subsequence","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/35148\/","title":{"rendered":"kotlin coding test course, longest common subsequence"},"content":{"rendered":"<p><body><\/p>\n<article>\n<section>\n<h2>1. Introduction<\/h2>\n<p>\n                The key to algorithm problem solving is to develop the ability to understand and solve various problems. Among them, the Longest Common Subsequence (LCS) problem is a fundamental problem that frequently appears in several algorithm tests. In this article, we will take a closer look at the process of solving the LCS problem using Kotlin.\n            <\/p>\n<\/section>\n<section>\n<h2>2. Problem Description<\/h2>\n<p>\n                Given two strings, the problem is to find the length of the longest subsequence that appears in both strings without deleting any characters. In other words, it is to find the longest subsequence that can be formed while maintaining the order of characters in both strings.\n            <\/p>\n<h3>Example<\/h3>\n<ul>\n<li>String 1: &#8220;ABCBDAB&#8221;<\/li>\n<li>String 2: &#8220;BDCAB&#8221;<\/li>\n<li>Longest Common Subsequence: &#8220;BCAB&#8221; (Length: 4)<\/li>\n<\/ul>\n<\/section>\n<section>\n<h2>3. Approach to Problem Solving<\/h2>\n<p>\n                A common approach to solve the LCS problem is Dynamic Programming. Dynamic Programming is a methodology for solving complex problems by breaking them down into simpler subproblems. Here, we will compare each character of the two strings and calculate the maximum length.\n            <\/p>\n<h3>3.1. Defining the Dynamic Programming Table<\/h3>\n<p>\n                First, let the lengths of the two strings be <code>m<\/code> and <code>n<\/code>, and define <code>dp[i][j]<\/code> as the length of the LCS of the first <code>i<\/code> characters of string 1 and the first <code>j<\/code> characters of string 2. The size of this table is <code>(m+1) x (n+1)<\/code>. The first row and column of the table are initialized to 0.\n            <\/p>\n<h3>3.2. Setting the Recurrence Relation<\/h3>\n<p>\n                The table will be filled according to the following rules, comparing characters up to the last character of both strings:\n            <\/p>\n<ul>\n<li>When characters are the same: <code>dp[i][j] = dp[i-1][j-1] + 1<\/code><\/li>\n<li>When characters are different: <code>dp[i][j] = max(dp[i-1][j], dp[i][j-1])<\/code><\/li>\n<\/ul>\n<h3>3.3. Extracting the Result<\/h3>\n<p>\n                The last cell <code>dp[m][n]<\/code> of the completed <code>dp<\/code> table contains the length of the LCS for the two strings.\n            <\/p>\n<\/section>\n<section>\n<h2>4. Kotlin Implementation<\/h2>\n<p>\n                Now, based on the theoretical explanation, let&#8217;s calculate the LCS using Kotlin.\n            <\/p>\n<pre><code>\nfun longestCommonSubsequence(s1: String, s2: String): Int {\n    val m = s1.length\n    val n = s2.length\n    val dp = Array(m + 1) { IntArray(n + 1) }\n\n    for (i in 1..m) {\n        for (j in 1..n) {\n            if (s1[i - 1] == s2[j - 1]) {\n                dp[i][j] = dp[i - 1][j - 1] + 1\n            } else {\n                dp[i][j] = maxOf(dp[i - 1][j], dp[i][j - 1])\n            }\n        }\n    }\n\n    return dp[m][n]\n}\n\nfun main() {\n    val s1 = \"ABCBDAB\"\n    val s2 = \"BDCAB\"\n    println(\"Length of Longest Common Subsequence: ${longestCommonSubsequence(s1, s2)}\")\n}\n            <\/code><\/pre>\n<\/section>\n<section>\n<h2>5. Analysis<\/h2>\n<p>\n                The above code has a time complexity of O(m * n) and a space complexity of O(m * n). Therefore, as the length of the strings increases, performance may decrease. However, the LCS problem can be improved through various optimization techniques.\n            <\/p>\n<h3>5.1. Improving Space Efficiency<\/h3>\n<p>\n                Currently, a 2D array is used, but since only the previous row&#8217;s values are needed, it can also be solved with a 1D array. This can reduce the space complexity to O(n).\n            <\/p>\n<pre><code>\nfun longestCommonSubsequenceOptimized(s1: String, s2: String): Int {\n    val m = s1.length\n    val n = s2.length\n    val dp = IntArray(n + 1)\n\n    for (i in 1..m) {\n        val prev = IntArray(n + 1)\n        for (j in 1..n) {\n            if (s1[i - 1] == s2[j - 1]) {\n                dp[j] = prev[j - 1] + 1\n            } else {\n                dp[j] = maxOf(dp[j - 1], prev[j])\n            }\n            prev[j] = dp[j]\n        }\n    }\n    return dp[n]\n}\n            <\/code><\/pre>\n<\/section>\n<section>\n<h2>6. Conclusion<\/h2>\n<p>\n                The Longest Common Subsequence problem is a very important problem in algorithm study and code testing. It can be efficiently solved using dynamic programming, and we have learned that there are various optimization techniques. I hope this problem provides an opportunity to develop algorithmic thinking and practice Kotlin programming.\n            <\/p>\n<\/section>\n<footer>\n<p>Please refer to additional materials for methods to improve the code and further information on algorithms.<\/p>\n<\/footer>\n<\/article>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Introduction The key to algorithm problem solving is to develop the ability to understand and solve various problems. Among them, the Longest Common Subsequence (LCS) problem is a fundamental problem that frequently appears in several algorithm tests. In this article, we will take a closer look at the process of solving the LCS problem &hellip; <a href=\"https:\/\/atmokpo.com\/w\/35148\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;kotlin coding test course, 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":[106],"tags":[],"class_list":["post-35148","post","type-post","status-publish","format-standard","hentry","category----en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>kotlin coding test course, 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\/35148\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"kotlin coding test course, longest common subsequence - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"1. Introduction The key to algorithm problem solving is to develop the ability to understand and solve various problems. Among them, the Longest Common Subsequence (LCS) problem is a fundamental problem that frequently appears in several algorithm tests. In this article, we will take a closer look at the process of solving the LCS problem &hellip; \ub354 \ubcf4\uae30 &quot;kotlin coding test course, longest common subsequence&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/35148\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:36:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:44:49+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\/35148\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35148\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"kotlin coding test course, longest common subsequence\",\"datePublished\":\"2024-11-01T09:36:07+00:00\",\"dateModified\":\"2024-11-01T11:44:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35148\/\"},\"wordCount\":432,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Kotlin coding test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/35148\/\",\"url\":\"https:\/\/atmokpo.com\/w\/35148\/\",\"name\":\"kotlin coding test course, longest common subsequence - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:36:07+00:00\",\"dateModified\":\"2024-11-01T11:44:49+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35148\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/35148\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/35148\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"kotlin coding test course, 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":"kotlin coding test course, 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\/35148\/","og_locale":"ko_KR","og_type":"article","og_title":"kotlin coding test course, longest common subsequence - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"1. Introduction The key to algorithm problem solving is to develop the ability to understand and solve various problems. Among them, the Longest Common Subsequence (LCS) problem is a fundamental problem that frequently appears in several algorithm tests. In this article, we will take a closer look at the process of solving the LCS problem &hellip; \ub354 \ubcf4\uae30 \"kotlin coding test course, longest common subsequence\"","og_url":"https:\/\/atmokpo.com\/w\/35148\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:36:07+00:00","article_modified_time":"2024-11-01T11:44:49+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\/35148\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/35148\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"kotlin coding test course, longest common subsequence","datePublished":"2024-11-01T09:36:07+00:00","dateModified":"2024-11-01T11:44:49+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/35148\/"},"wordCount":432,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Kotlin coding test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/35148\/","url":"https:\/\/atmokpo.com\/w\/35148\/","name":"kotlin coding test course, longest common subsequence - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:36:07+00:00","dateModified":"2024-11-01T11:44:49+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/35148\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/35148\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/35148\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"kotlin coding test course, 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\/35148","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=35148"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/35148\/revisions"}],"predecessor-version":[{"id":35149,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/35148\/revisions\/35149"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=35148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=35148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=35148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}