Swift Coding Test Course, DNA Password

This article deals with algorithm problems that are frequently presented in coding tests, and explains in depth the process of solving those problems. Today’s topic is ‘DNA Password’, where we will solve a password problem based on DNA strings.

Problem Description

You need to design a system that generates passwords using DNA strings. The DNA string consists of uppercase letters A, C, G, and T, and each character becomes a component of the password in various combinations.

You are to find specific patterns in the given DNA string and generate a password based on these patterns. The following is a detailed problem definition:

Problem Definition

Given a string s and a natural number k, you should consider all substrings of length k from s and generate a password using the frequency of A, C, G, and T in these substrings.

The password is defined as the count of substrings where the frequency of A, C, G, and T is at least min_count.

Input Example

s = "ACGTACGTGACG", k = 4, min_count = 1

Output Example

3

Here, “ACGT”, “CGTA”, and “GTAC” are substrings with frequencies of at least 1.

Problem Solving Strategy

The strategy to solve this problem is as follows:

  1. Generate substrings of length k from the entire string s.
  2. Calculate the frequencies of A, C, G, and T for each substring.
  3. If the frequencies of A, C, G, and T are at least min_count, increase the count.
  4. Finally, output the count.

Implementation Steps

Now let’s look at how to implement the problem using Swift. Here is the step-by-step code to solve the problem.

1. Input

First, we take the string and variables as input. In Swift, values can be received through command-line arguments or direct input.


let inputString = "ACGTACGTGACG"
let k = 4
let minCount = 1

2. Generate Substrings

Generate all substrings of length k from the string.


var count = 0
let length = inputString.count

for i in 0...(length - k) {
    let startIndex = inputString.index(inputString.startIndex, offsetBy: i)
    let endIndex = inputString.index(startIndex, offsetBy: k)
    let substring = String(inputString[startIndex..

3. Frequency Calculation

Calculate the frequencies of each character (A, C, G, T) for the generated substrings.


var frequencies = [Character: Int]()

for char in substring {
    frequencies[char, default: 0] += 1
}

// Condition Check
if frequencies["A"]! >= minCount && 
   frequencies["C"]! >= minCount && 
   frequencies["G"]! >= minCount && 
   frequencies["T"]! >= minCount {
    count += 1
}

4. Output Result

Print the final count.


print("Total valid passwords: \(count)")

Complete Code

Now let's take a look at the complete code that integrates all the steps.


let inputString = "ACGTACGTGACG"
let k = 4
let minCount = 1
var count = 0
let length = inputString.count

for i in 0...(length - k) {
    let startIndex = inputString.index(inputString.startIndex, offsetBy: i)
    let endIndex = inputString.index(startIndex, offsetBy: k)
    let substring = String(inputString[startIndex..= minCount && 
       frequencies["C"]! >= minCount && 
       frequencies["G"]! >= minCount && 
       frequencies["T"]! >= minCount {
        count += 1
    }
}

print("Total valid passwords: \(count)")

Result

Running this code with the given input will output the number of valid passwords. Understanding the basic algorithm patterns of handling strings and checking conditions is very useful in coding tests.

Furthermore, you can enhance your problem-solving skills by testing the code against various input cases and adding exception handling.

I hope this article helps you prepare for coding tests using Swift. Keep practicing and solving problems to improve your skills!