{"id":33922,"date":"2024-11-01T09:22:03","date_gmt":"2024-11-01T09:22:03","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33922"},"modified":"2024-11-01T10:55:02","modified_gmt":"2024-11-01T10:55:02","slug":"c-coding-test-course-string-search","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33922\/","title":{"rendered":"C# Coding Test Course, String Search"},"content":{"rendered":"<p><body><\/p>\n<h2>Problem Description<\/h2>\n<p>You need to find out how many times a specific string P appears in the string S. String P can appear multiple times in string S, and there may be overlapping occurrences. The length of the given string S is between 1 and 100,000, and the length of string P is between 1 and 100. The comparison is case-insensitive.<\/p>\n<h2>Input Format<\/h2>\n<ul>\n<li>First line: string S (1 \u2264 |S| \u2264 100,000)<\/li>\n<li>Second line: string P (1 \u2264 |P| \u2264 100)<\/li>\n<\/ul>\n<h2>Output Format<\/h2>\n<p>Print the total number of occurrences as an integer.<\/p>\n<h3>Example<\/h3>\n<h4>Input<\/h4>\n<pre>\n    abCabcABCabc\n    abc\n    <\/pre>\n<h4>Output<\/h4>\n<pre>\n    4\n    <\/pre>\n<h2>Problem Solving Process<\/h2>\n<h3>1. Understanding the Problem<\/h3>\n<p>This problem involves checking how many times string P appears in the given string S. Since string comparison is case-insensitive, both strings should be converted to lowercase for comparison.<\/p>\n<h3>2. Approach<\/h3>\n<p>There are several approaches to string search problems, but we will solve it directly using a simple loop. The following steps will be taken to solve the problem:<\/p>\n<ol>\n<li>Convert string S to lowercase.<\/li>\n<li>Convert string P to lowercase.<\/li>\n<li>Use a loop to find string P in string S.<\/li>\n<li>Increment the count for each occurrence.<\/li>\n<\/ol>\n<h3>3. Algorithm Design<\/h3>\n<p>The complexity of the algorithm is O(n*m), where n is the length of string S and m is the length of string P. Since we are directly comparing the two strings, in the worst case we may need to search string P from every index.<\/p>\n<h3>4. C# Code Implementation<\/h3>\n<p>Below is an example of C# code.<\/p>\n<pre>\n    using System;\n\n    class Program\n    {\n        static void Main(string[] args)\n        {\n            string S = Console.ReadLine();\n            string P = Console.ReadLine();\n\n            \/\/ Convert to lowercase for case-insensitive comparison\n            S = S.ToLower();\n            P = P.ToLower();\n\n            int count = 0;\n            int position = 0;\n\n            while ((position = S.IndexOf(P, position)) != -1)\n            {\n                count++;\n                position++; \/\/ Move position by one to consider overlapping cases\n            }\n\n            Console.WriteLine(count);\n        }\n    }\n    <\/pre>\n<h3>5. Code Explanation<\/h3>\n<p>The code includes the following steps:<\/p>\n<ol>\n<li>Accept input strings S and P from the user.<\/li>\n<li>Convert strings S and P to lowercase for easier comparison.<\/li>\n<li>Use a while loop to find the position of P in string S.<\/li>\n<li>Use the S.IndexOf() method to locate P starting from the current position. If the found position is not -1, increase the count and move to the next position.<\/li>\n<li>Output the total number of occurrences.<\/li>\n<\/ol>\n<h2>Performance Considerations<\/h2>\n<p>The time complexity of this code is O(n*m), which varies depending on the lengths of strings S and P. If the length of S is 100,000 and P is 100, the worst case could require 10,000,000 operations. This may be somewhat inefficient.<\/p>\n<p>Therefore, if you wish to further improve performance, you might consider using string search algorithms like KMP (Knuth-Morris-Pratt). The KMP algorithm has a time complexity of O(n + m) and enables more efficient searching.<\/p>\n<h3>5-1. Overview of the KMP Algorithm<\/h3>\n<p>The KMP algorithm is an efficient method for substring searching. It operates on the following principles:<\/p>\n<ul>\n<li>First, it creates an array to store the partial matches of the pattern string P.<\/li>\n<li>While scanning string S, it calculates how many characters can be skipped in the pattern string when a mismatch occurs.<\/li>\n<\/ul>\n<h3>5-2. KMP Algorithm C# Implementation<\/h3>\n<p>Below is the C# code implementing the KMP algorithm.<\/p>\n<pre>\n    using System;\n\n    class Program\n    {\n        static void Main(string[] args)\n        {\n            string S = Console.ReadLine();\n            string P = Console.ReadLine();\n\n            \/\/ Convert to lowercase for case-insensitive comparison\n            S = S.ToLower();\n            P = P.ToLower();\n\n            int count = KMP(S, P);\n            Console.WriteLine(count);\n        }\n\n        static int KMP(string S, string P)\n        {\n            int m = P.Length;\n            int n = S.Length;\n            int count = 0;\n\n            \/\/ Initialize LPS array\n            int[] lps = new int[m];\n            ComputeLPSArray(P, m, lps);\n\n            int i = 0; \/\/ Index for S\n            int j = 0; \/\/ Index for P\n\n            while (i < n)\n            {\n                if (P[j] == S[i])\n                {\n                    i++;\n                    j++;\n                }\n\n                if (j == m)\n                {\n                    count++;\n                    j = lps[j - 1];\n                }\n                else if (i < n &#038;&#038; P[j] != S[i])\n                {\n                    if (j != 0)\n                        j = lps[j - 1];\n                    else\n                        i++;\n                }\n            }\n            return count;\n        }\n\n        static void ComputeLPSArray(string P, int m, int[] lps)\n        {\n            int len = 0;\n            int i = 1;\n            lps[0] = 0;\n\n            while (i < m)\n            {\n                if (P[i] == P[len])\n                {\n                    len++;\n                    lps[i] = len;\n                    i++;\n                }\n                else\n                {\n                    if (len != 0)\n                        len = lps[len - 1];\n                    else\n                    {\n                        lps[i] = 0;\n                        i++;\n                    }\n                }\n            }\n        }\n    }\n    <\/pre>\n<h3>6. KMP Algorithm Code Explanation<\/h3>\n<p>The above code operates in the following manner:<\/p>\n<ol>\n<li>First, it converts strings S and P to lowercase to eliminate case sensitivity.<\/li>\n<li>Calls the KMP method to explore how many times string P appears in string S.<\/li>\n<li>Inside the KMP method, it generates the LPS array. The LPS array stores the maximum length of the prefix and suffix of pattern P.<\/li>\n<li>While scanning string S, it matches the pattern P. If matching is successful, it increments the count, and if matching fails, it adjusts the position based on the LPS array.<\/li>\n<\/ol>\n<h2>Conclusion<\/h2>\n<p>In this lecture, we learned how to solve the problem of finding a specific substring in a string using C#. From a simple loop approach to the extension using the KMP algorithm, we gained an understanding of the fundamental concepts of string searching and efficient approaches. I hope this process helped you understand various coding implementations and the complexities of algorithms.<\/p>\n<h2>References<\/h2>\n<ul>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/String_searching_algorithm\">String searching algorithm - Wikipedia<\/a><\/li>\n<li><a href=\"https:\/\/www.geeksforgeeks.org\/kmp-algorithm-for-pattern-searching\/\">KMP Algorithm - GeeksforGeeks<\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/17292073\/counting-substring-occurrences-in-c\">Counting substring occurrences in C# - Stack Overflow<\/a><\/li>\n<\/ul>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Description You need to find out how many times a specific string P appears in the string S. String P can appear multiple times in string S, and there may be overlapping occurrences. The length of the given string S is between 1 and 100,000, and the length of string P is between 1 &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33922\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C# 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":[90],"tags":[],"class_list":["post-33922","post","type-post","status-publish","format-standard","hentry","category-c-coding-test-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C# 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\/33922\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C# Coding Test Course, String Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Problem Description You need to find out how many times a specific string P appears in the string S. String P can appear multiple times in string S, and there may be overlapping occurrences. The length of the given string S is between 1 and 100,000, and the length of string P is between 1 &hellip; \ub354 \ubcf4\uae30 &quot;C# Coding Test Course, String Search&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33922\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:22:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:55: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=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/33922\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33922\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C# Coding Test Course, String Search\",\"datePublished\":\"2024-11-01T09:22:03+00:00\",\"dateModified\":\"2024-11-01T10:55:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33922\/\"},\"wordCount\":635,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C# Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33922\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33922\/\",\"name\":\"C# Coding Test Course, String Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:22:03+00:00\",\"dateModified\":\"2024-11-01T10:55:02+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33922\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33922\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33922\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C# 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":"C# 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\/33922\/","og_locale":"ko_KR","og_type":"article","og_title":"C# Coding Test Course, String Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Problem Description You need to find out how many times a specific string P appears in the string S. String P can appear multiple times in string S, and there may be overlapping occurrences. The length of the given string S is between 1 and 100,000, and the length of string P is between 1 &hellip; \ub354 \ubcf4\uae30 \"C# Coding Test Course, String Search\"","og_url":"https:\/\/atmokpo.com\/w\/33922\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:22:03+00:00","article_modified_time":"2024-11-01T10:55: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":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/33922\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33922\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C# Coding Test Course, String Search","datePublished":"2024-11-01T09:22:03+00:00","dateModified":"2024-11-01T10:55:02+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33922\/"},"wordCount":635,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C# Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33922\/","url":"https:\/\/atmokpo.com\/w\/33922\/","name":"C# Coding Test Course, String Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:22:03+00:00","dateModified":"2024-11-01T10:55:02+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33922\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33922\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33922\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C# 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\/33922","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=33922"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33922\/revisions"}],"predecessor-version":[{"id":33923,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33922\/revisions\/33923"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33922"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33922"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}