{"id":34504,"date":"2024-11-01T09:28:44","date_gmt":"2024-11-01T09:28:44","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34504"},"modified":"2024-11-01T11:41:00","modified_gmt":"2024-11-01T11:41:00","slug":"javascript-coding-test-course-string-search","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34504\/","title":{"rendered":"JavaScript Coding Test Course, String Search"},"content":{"rendered":"<p><body><\/p>\n<h2>Problem Description<\/h2>\n<p>\n        There is a given string <code>text<\/code> and a string <code>pattern<\/code> that needs to be found.<br \/>\n        Write a function that returns the position where <code>pattern<\/code> first appears in <code>text<\/code>.<br \/>\n        If the <code>pattern<\/code> does not exist in <code>text<\/code>, return -1.\n    <\/p>\n<h3>Input Example<\/h3>\n<ul>\n<li><strong>text:<\/strong> &#8220;hello world&#8221;<\/li>\n<li><strong>pattern:<\/strong> &#8220;world&#8221;<\/li>\n<\/ul>\n<h3>Output Example<\/h3>\n<ul>\n<li><strong>Result:<\/strong> 6 (the string &#8220;world&#8221; starts at index 6)<\/li>\n<\/ul>\n<h2>Problem Solving Strategy<\/h2>\n<p>\n        To solve this problem, we need to check if a specific pattern exists within the string and,<br \/>\n        if it does, find its starting index. There are various algorithms for string searching, but<br \/>\n        in this tutorial, we will use the most basic and intuitive method called &#8216;Brute Force&#8217; and<br \/>\n        a more efficient algorithm called &#8216;KMP (Knuth-Morris-Pratt)&#8217;.<br \/>\n        Let&#8217;s first take a look at the Brute Force method.\n    <\/p>\n<h3>Brute Force Approach<\/h3>\n<p>\n        The Brute Force method compares all combinations of the given string and the pattern to be found.<br \/>\n        This method is simple and easy to understand, but in the worst case, its time complexity is O(n*m),<br \/>\n        where n is the length of the text and m is the length of the pattern.\n    <\/p>\n<h4>Algorithm Steps<\/h4>\n<ol>\n<li>Set the variable for the length of the text (n) and the length of the pattern (m).<\/li>\n<li>Increase the starting position of the text one by one and compare with the pattern from the current position.<\/li>\n<li>If all character comparisons match, return the current starting index.<\/li>\n<li>If all characters are compared and the pattern is not found, return -1.<\/li>\n<\/ol>\n<h4>JavaScript Code Implementation<\/h4>\n<pre><code>\nfunction findFirstOccurrence(text, pattern) {\n    const n = text.length;\n    const m = pattern.length;\n\n    for (let i = 0; i &lt;= n - m; i++) {\n        let j;\n        for (j = 0; j &lt; m; j++) {\n            if (text[i + j] !== pattern[j]) {\n                break;\n            }\n        }\n        if (j === m) {\n            return i; \/\/ Pattern found\n        }\n    }\n    return -1; \/\/ Pattern not found\n}\n\n\/\/ Test examples\nconsole.log(findFirstOccurrence(\"hello world\", \"world\")); \/\/ 6\nconsole.log(findFirstOccurrence(\"hello world\", \"abc\")); \/\/ -1\n    <\/code><\/pre>\n<h2>KMP Algorithm<\/h2>\n<p>\n        While the Brute Force method is simple, it can be inefficient. The KMP algorithm improves performance by<br \/>\n        preventing unnecessary re-inspections during the search. The basic concept of the KMP algorithm is<br \/>\n        &#8216;when part of the pattern matches, reuse the remainder&#8217;.\n    <\/p>\n<h3>Principle of the KMP Algorithm<\/h3>\n<p>\n        The KMP algorithm optimizes string searching using a &#8216;partial match table (or failure function)&#8217;.<br \/>\n        This table provides information that can be cached during the search.<br \/>\n        The time complexity of the KMP algorithm is O(n + m), making it effective for large datasets.\n    <\/p>\n<h4>Algorithm Steps<\/h4>\n<ol>\n<li>Create a partial match table for the pattern.<\/li>\n<li>While comparing text and pattern, if there is a mismatch, refer to the table to specify the pattern&#8217;s position.<\/li>\n<li>Repeat until a match is found, and ultimately return the index.<\/li>\n<\/ol>\n<h4>Creating the Partial Match Table<\/h4>\n<p>\n        The algorithm for creating the partial match table is as follows. This table is used to adjust<br \/>\n        the index for the next comparison based on the same prefixes and suffixes from the previously examined string.\n    <\/p>\n<pre><code>\nfunction buildKMPTable(pattern) {\n    const m = pattern.length;\n    const lps = new Array(m).fill(0);\n    let len = 0; \n    let i = 1;\n\n    while (i &lt; m) {\n        if (pattern[i] === pattern[len]) {\n            len++;\n            lps[i] = len;\n            i++;\n        } else {\n            if (len !== 0) {\n                len = lps[len - 1];\n            } else {\n                lps[i] = 0;\n                i++;\n            }\n        }\n    }\n    return lps;\n}\n    <\/code><\/pre>\n<h4>KMP Algorithm Code Implementation<\/h4>\n<pre><code>\nfunction KMPSearch(text, pattern) {\n    const n = text.length;\n    const m = pattern.length;\n    const lps = buildKMPTable(pattern);\n    let i = 0; \/\/ Text index\n    let j = 0; \/\/ Pattern index\n\n    while (i &lt; n) {\n        if (pattern[j] === text[i]) {\n            i++;\n            j++;\n        }\n        if (j === m) {\n            return i - j; \/\/ Pattern found\n        } else if (i &lt; n &amp;&amp; pattern[j] !== text[i]) {\n            if (j !== 0) {\n                j = lps[j - 1];\n            } else {\n                i++;\n            }\n        }\n    }\n    return -1; \/\/ Pattern not found\n}\n\n\/\/ Test examples\nconsole.log(KMPSearch(\"hello world\", \"world\")); \/\/ 6\nconsole.log(KMPSearch(\"hello world\", \"abc\")); \/\/ -1\n    <\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>\n        In this tutorial, we explored two algorithms for solving the string search problem,<br \/>\n        the Brute Force method, and the KMP algorithm.<br \/>\n        The Brute Force method is intuitive and straightforward but can be inefficient when searching large strings.<br \/>\n        In contrast, the KMP algorithm provides a more efficient way to search for patterns.<br \/>\n        Understanding and appropriately utilizing these diverse algorithms is important in real coding tests.\n    <\/p>\n<p>\n        Problems related to string searching are frequently featured in coding tests, so<br \/>\n        it&#8217;s necessary to gain experience by solving various examples.<br \/>\n        Keep learning different algorithm problems to further enhance your skills.\n    <\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Description There is a given string text and a string pattern that needs to be found. Write a function that returns the position where pattern first appears in text. If the pattern does not exist in text, return -1. Input Example text: &#8220;hello world&#8221; pattern: &#8220;world&#8221; Output Example Result: 6 (the string &#8220;world&#8221; starts &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34504\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;JavaScript 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":[141],"tags":[],"class_list":["post-34504","post","type-post","status-publish","format-standard","hentry","category-javascript-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JavaScript 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\/34504\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Coding Test Course, String Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Problem Description There is a given string text and a string pattern that needs to be found. Write a function that returns the position where pattern first appears in text. If the pattern does not exist in text, return -1. Input Example text: &#8220;hello world&#8221; pattern: &#8220;world&#8221; Output Example Result: 6 (the string &#8220;world&#8221; starts &hellip; \ub354 \ubcf4\uae30 &quot;JavaScript Coding Test Course, String Search&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34504\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:28:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:41:00+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=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/34504\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34504\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"JavaScript Coding Test Course, String Search\",\"datePublished\":\"2024-11-01T09:28:44+00:00\",\"dateModified\":\"2024-11-01T11:41:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34504\/\"},\"wordCount\":516,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Javascript Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34504\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34504\/\",\"name\":\"JavaScript Coding Test Course, String Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:28:44+00:00\",\"dateModified\":\"2024-11-01T11:41:00+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34504\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34504\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34504\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript 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":"JavaScript 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\/34504\/","og_locale":"ko_KR","og_type":"article","og_title":"JavaScript Coding Test Course, String Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Problem Description There is a given string text and a string pattern that needs to be found. Write a function that returns the position where pattern first appears in text. If the pattern does not exist in text, return -1. Input Example text: &#8220;hello world&#8221; pattern: &#8220;world&#8221; Output Example Result: 6 (the string &#8220;world&#8221; starts &hellip; \ub354 \ubcf4\uae30 \"JavaScript Coding Test Course, String Search\"","og_url":"https:\/\/atmokpo.com\/w\/34504\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:28:44+00:00","article_modified_time":"2024-11-01T11:41:00+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":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/34504\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34504\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"JavaScript Coding Test Course, String Search","datePublished":"2024-11-01T09:28:44+00:00","dateModified":"2024-11-01T11:41:00+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34504\/"},"wordCount":516,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Javascript Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34504\/","url":"https:\/\/atmokpo.com\/w\/34504\/","name":"JavaScript Coding Test Course, String Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:28:44+00:00","dateModified":"2024-11-01T11:41:00+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34504\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34504\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34504\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"JavaScript 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\/34504","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=34504"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34504\/revisions"}],"predecessor-version":[{"id":34505,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34504\/revisions\/34505"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34504"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34504"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34504"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}