{"id":33355,"date":"2024-11-01T09:15:50","date_gmt":"2024-11-01T09:15:50","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33355"},"modified":"2024-11-01T11:39:02","modified_gmt":"2024-11-01T11:39:02","slug":"java-coding-test-course-string-search","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33355\/","title":{"rendered":"Java Coding Test Course, String Search"},"content":{"rendered":"<p><body><\/p>\n<h2>Problem Description<\/h2>\n<p>\n        This is a problem to find how many times a specific pattern appears in a given string.<br \/>\n        For example, determining how many times the pattern <code>\"ana\"<\/code> appears in the string <code>\"banana\"<\/code>.\n    <\/p>\n<h3>Input<\/h3>\n<ul>\n<li>String <code>text<\/code>: The entire string to search (1 \u2264 |text| \u2264 100,000)<\/li>\n<li>String <code>pattern<\/code>: The string to find (1 \u2264 |pattern| \u2264 50)<\/li>\n<\/ul>\n<h3>Output<\/h3>\n<p>\n        Returns the total count of how many times <code>pattern<\/code> appears in the string <code>text<\/code>.\n    <\/p>\n<h2>Example<\/h2>\n<h3>Input<\/h3>\n<pre><code>text = \"banana\"\npattern = \"ana\"<\/code><\/pre>\n<h3>Output<\/h3>\n<pre><code>2<\/code><\/pre>\n<h2>Solution Process<\/h2>\n<p>\n        To solve this problem, a string search algorithm must be used.<br \/>\n        The simplest way is to traverse the string one character at a time and compare the pattern, but this is inefficient and has a time complexity of O(n*m) for large data.\n    <\/p>\n<h3>Efficient Solution: KMP Algorithm<\/h3>\n<p>\n        One of the ways to efficiently solve the string search problem is by using the KMP (Knuth-Morris-Pratt) algorithm.<br \/>\n        This algorithm consists of the following two parts:\n    <\/p>\n<ol>\n<li>Create an &#8216;LPS (Longest Prefix which is also Suffix)&#8217; array based on the pattern.<\/li>\n<li>Traverse the text, comparing the pattern while utilizing the LPS array.<\/li>\n<\/ol>\n<h4>Generating the LPS Array<\/h4>\n<p>\n        The LPS array indicates how much the prefix and suffix match within the pattern. This allows for the pattern to be reused during the search.<br \/>\n        For example, if the pattern is <code>\"ABAB\"<\/code>, the LPS array will be <code>[0, 0, 1, 2]<\/code>.\n    <\/p>\n<h4>Algorithm Implementation<\/h4>\n<p>Now, based on the above process, we will implement the string search algorithm in Java.<\/p>\n<pre><code>public class KMP {\n    public static int[] computeLPS(String pattern) {\n        int[] lps = new int[pattern.length()];\n        int length = 0; \n        int i = 1;\n        \n        while (i &lt; pattern.length()) {\n            if (pattern.charAt(i) == pattern.charAt(length)) {\n                length++;\n                lps[i] = length;\n                i++;\n            } else {\n                if (length != 0) {\n                    length = lps[length - 1];\n                } else {\n                    lps[i] = 0;\n                    i++;\n                }\n            }\n        }\n        return lps;\n    }\n\n    public static int KMPsearch(String text, String pattern) {\n        int[] lps = computeLPS(pattern);\n        int i = 0; \n        int j = 0; \n        int count = 0;\n\n        while (i &lt; text.length()) {\n            if (pattern.charAt(j) == text.charAt(i)) {\n                i++;\n                j++;\n\n                if (j == pattern.length()) {\n                    count++;\n                    j = lps[j - 1];\n                }\n            } else {\n                if (j != 0) {\n                    j = lps[j - 1];\n                } else {\n                    i++;\n                }\n            }\n        }\n        return count;\n    }\n\n    public static void main(String[] args) {\n        String text = \"banana\";\n        String pattern = \"ana\";\n        int result = KMPsearch(text, pattern);\n        System.out.println(\"Substring Count: \" + result);\n    }\n}<\/code><\/pre>\n<h2>Code Explanation<\/h2>\n<p>\n        The code above implements the KMP algorithm, which consists of two main functions.<br \/>\n        The <code>computeLPS<\/code> function generates the LPS array for the pattern, and the <code>KMPsearch<\/code> function searches for the pattern in the text and counts the matches.\n    <\/p>\n<h2>Complexity Analysis<\/h2>\n<p>\n        The time complexity of the KMP algorithm is O(n + m). Here, n is the length of the text and m is the length of the pattern.<br \/>\n        This is an efficient method because the pattern moves and is reused within the text.\n    <\/p>\n<h2>Conclusion<\/h2>\n<p>\n        Today, we explored the KMP algorithm to solve the string search problem.<br \/>\n        By using this algorithm, we can efficiently tackle various string search problems.<br \/>\n        As you encounter problems, try experimenting with different algorithms to build your skills!\n    <\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Description This is a problem to find how many times a specific pattern appears in a given string. For example, determining how many times the pattern &#8220;ana&#8221; appears in the string &#8220;banana&#8221;. Input String text: The entire string to search (1 \u2264 |text| \u2264 100,000) String pattern: The string to find (1 \u2264 |pattern| &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33355\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Java Coding Test Course, String Search&#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":[139],"tags":[],"class_list":["post-33355","post","type-post","status-publish","format-standard","hentry","category-java-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java Coding Test Course, String Search - \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\/33355\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Coding Test Course, String Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Problem Description This is a problem to find how many times a specific pattern appears in a given string. For example, determining how many times the pattern &quot;ana&quot; appears in the string &quot;banana&quot;. Input String text: The entire string to search (1 \u2264 |text| \u2264 100,000) String pattern: The string to find (1 \u2264 |pattern| &hellip; \ub354 \ubcf4\uae30 &quot;Java Coding Test Course, String Search&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33355\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:15:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:39:02+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=\"2\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/33355\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33355\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Java Coding Test Course, String Search\",\"datePublished\":\"2024-11-01T09:15:50+00:00\",\"dateModified\":\"2024-11-01T11:39:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33355\/\"},\"wordCount\":345,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Java Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33355\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33355\/\",\"name\":\"Java Coding Test Course, String Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:15:50+00:00\",\"dateModified\":\"2024-11-01T11:39:02+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33355\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33355\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33355\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Coding Test Course, String Search\"}]},{\"@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":"Java Coding Test Course, String Search - \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\/33355\/","og_locale":"ko_KR","og_type":"article","og_title":"Java Coding Test Course, String Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Problem Description This is a problem to find how many times a specific pattern appears in a given string. For example, determining how many times the pattern \"ana\" appears in the string \"banana\". Input String text: The entire string to search (1 \u2264 |text| \u2264 100,000) String pattern: The string to find (1 \u2264 |pattern| &hellip; \ub354 \ubcf4\uae30 \"Java Coding Test Course, String Search\"","og_url":"https:\/\/atmokpo.com\/w\/33355\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:15:50+00:00","article_modified_time":"2024-11-01T11:39:02+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":"2\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/33355\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33355\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Java Coding Test Course, String Search","datePublished":"2024-11-01T09:15:50+00:00","dateModified":"2024-11-01T11:39:02+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33355\/"},"wordCount":345,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Java Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33355\/","url":"https:\/\/atmokpo.com\/w\/33355\/","name":"Java Coding Test Course, String Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:15:50+00:00","dateModified":"2024-11-01T11:39:02+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33355\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33355\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33355\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Java Coding Test Course, String Search"}]},{"@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\/33355","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=33355"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33355\/revisions"}],"predecessor-version":[{"id":33356,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33355\/revisions\/33356"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}