{"id":33291,"date":"2024-11-01T09:15:13","date_gmt":"2024-11-01T09:15:13","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33291"},"modified":"2024-11-01T11:39:19","modified_gmt":"2024-11-01T11:39:19","slug":"java-coding-test-course-dna-password","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33291\/","title":{"rendered":"Java Coding Test Course, DNA Password"},"content":{"rendered":"<p><body><\/p>\n<h2>Problem Description<\/h2>\n<p>With the recent advancements in DNA analysis technology, a password system based on genetic information has been proposed. In this system, a DNA sequence of a certain length is used as a password. DNA consists of 4 nucleotides: <strong>A<\/strong>, <strong>T<\/strong>, <strong>C<\/strong>, and <strong>G<\/strong>. You need to find subsequences from a given DNA sequence that can be valid passwords. Each password must pass a validity check based on certain rules, which require a minimum and maximum occurrence of specific nucleotides.<\/p>\n<h3>Problem<\/h3>\n<p>Write a program that counts the number of valid passwords based on a given DNA string and the minimum and maximum occurrences of each nucleotide (A, T, C, G).<\/p>\n<h3>Input Format<\/h3>\n<ul>\n<li>The first line contains the DNA string. (1 \u2264 DNA.length \u2264 1000)<\/li>\n<li>The second line contains the minimum and maximum occurrence counts of each nucleotide (A, T, C, G) separated by spaces.<\/li>\n<\/ul>\n<h3>Output Format<\/h3>\n<p>Print the number of valid passwords.<\/p>\n<h2>Example<\/h2>\n<div class=\"example\">\n<strong>Input:<\/strong><br \/>\n        AGCTTAGCTG<br \/>\n        1 2 1 2 (Minimum and maximum occurrence counts for each nucleotide)<br \/>\n<strong>Output:<\/strong><br \/>\n        6\n    <\/div>\n<h2>Problem-Solving Process<\/h2>\n<p>To solve this problem, we will follow the steps outlined below.<\/p>\n<h3>1. Problem Analysis<\/h3>\n<p>Since the password must satisfy validity conditions, we need a way to count the occurrences of each nucleotide in the subsequence. After generating all the substrings of the given DNA string, we need to check the occurrences in each substring.<\/p>\n<h3>2. Sliding Window Technique<\/h3>\n<p>Checking all substrings is inefficient due to the maximum string length of 1000. We will use a technique called <strong>sliding window<\/strong>. This technique maintains a combination of indices that represent a portion of the array or string, expanding or contracting as necessary to the left or right.<\/p>\n<h3>3. Exploring Implementation Methods<\/h3>\n<p>Using the sliding window, we will adjust the length of the subsequence while checking the counts of each nucleotide. During this process, we will verify if the counts meet the conditions and count valid passwords.<\/p>\n<h3>4. Code Writing<\/h3>\n<pre><code>public class DNAPassword {\n        public static void main(String[] args) {\n            String dna = \"AGCTTAGCTG\";\n            int[] minCounts = {1, 2, 1, 2}; \/\/ Minimum occurrence counts for A, T, C, G\n            int[] maxCounts = {2, 3, 2, 3}; \/\/ Maximum occurrence counts for A, T, C, G\n            \n            System.out.println(validPasswordCount(dna, minCounts, maxCounts));\n        }\n\n        public static int validPasswordCount(String dna, int[] minCounts, int[] maxCounts) {\n            int[] counts = new int[4]; \/\/ Counts for each nucleotide\n            int validCount = 0;\n            int left = 0;\n\n            for (int right = 0; right &lt; dna.length(); right++) {\n                counts[getIndex(dna.charAt(right))]++;\n                \n                while (isValid(counts, minCounts, maxCounts)) {\n                    validCount++;\n                    counts[getIndex(dna.charAt(left))]--;\n                    left++;\n                }\n            }\n            return validCount;\n        }\n\n        private static int getIndex(char c) {\n            switch (c) {\n                case 'A': return 0;\n                case 'T': return 1;\n                case 'C': return 2;\n                case 'G': return 3;\n                default: return -1;\n            }\n        }\n\n        private static boolean isValid(int[] counts, int[] minCounts, int[] maxCounts) {\n            for (int i = 0; i &lt; 4; i++) {\n                if (counts[i] &lt; minCounts[i] || counts[i] &gt; maxCounts[i]) {\n                    return false;\n                }\n            }\n            return true;\n        }\n    }<\/code><\/pre>\n<h3>5. Testing and Verification<\/h3>\n<p>After writing the code, experiment with the input values as shown in the example to verify that the number of valid passwords is correctly printed. Test various DNA strings and count values that meet the given conditions to check the reliability of the program.<\/p>\n<h2>Result Analysis<\/h2>\n<p>The number of valid passwords outputted as a result must actually match the answer provided in the problem. Additionally, set various test cases to confirm that the results are as expected.<\/p>\n<h2>Conclusion<\/h2>\n<p>In this course, we have covered the DNA password problem as an example of a Java coding test. I hope you have gained an understanding of how useful the sliding window technique can be in solving algorithmic problems. In the next course, we will cover more useful algorithms!<\/p>\n<footer>\n<p><strong>Author:<\/strong> Algorithm Instructor<\/p>\n<p><strong>Date:<\/strong> October 10, 2023<\/p>\n<\/footer>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Description With the recent advancements in DNA analysis technology, a password system based on genetic information has been proposed. In this system, a DNA sequence of a certain length is used as a password. DNA consists of 4 nucleotides: A, T, C, and G. You need to find subsequences from a given DNA sequence &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33291\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Java Coding Test Course, DNA Password&#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-33291","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, DNA Password - \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\/33291\/\" \/>\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, DNA Password - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Problem Description With the recent advancements in DNA analysis technology, a password system based on genetic information has been proposed. In this system, a DNA sequence of a certain length is used as a password. DNA consists of 4 nucleotides: A, T, C, and G. You need to find subsequences from a given DNA sequence &hellip; \ub354 \ubcf4\uae30 &quot;Java Coding Test Course, DNA Password&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33291\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:15:13+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:39:19+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\/33291\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33291\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Java Coding Test Course, DNA Password\",\"datePublished\":\"2024-11-01T09:15:13+00:00\",\"dateModified\":\"2024-11-01T11:39:19+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33291\/\"},\"wordCount\":445,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Java Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33291\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33291\/\",\"name\":\"Java Coding Test Course, DNA Password - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:15:13+00:00\",\"dateModified\":\"2024-11-01T11:39:19+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33291\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33291\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33291\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Coding Test Course, DNA Password\"}]},{\"@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, DNA Password - \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\/33291\/","og_locale":"ko_KR","og_type":"article","og_title":"Java Coding Test Course, DNA Password - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Problem Description With the recent advancements in DNA analysis technology, a password system based on genetic information has been proposed. In this system, a DNA sequence of a certain length is used as a password. DNA consists of 4 nucleotides: A, T, C, and G. You need to find subsequences from a given DNA sequence &hellip; \ub354 \ubcf4\uae30 \"Java Coding Test Course, DNA Password\"","og_url":"https:\/\/atmokpo.com\/w\/33291\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:15:13+00:00","article_modified_time":"2024-11-01T11:39:19+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\/33291\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33291\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Java Coding Test Course, DNA Password","datePublished":"2024-11-01T09:15:13+00:00","dateModified":"2024-11-01T11:39:19+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33291\/"},"wordCount":445,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Java Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33291\/","url":"https:\/\/atmokpo.com\/w\/33291\/","name":"Java Coding Test Course, DNA Password - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:15:13+00:00","dateModified":"2024-11-01T11:39:19+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33291\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33291\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33291\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Java Coding Test Course, DNA Password"}]},{"@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\/33291","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=33291"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33291\/revisions"}],"predecessor-version":[{"id":33292,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33291\/revisions\/33292"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}