Swift Coding Test Course, Find the Number of Colloquial Expressions

Author: [Author Name]

Date: [Date]

1. What is a Binary Friend Number?

A Binary Friend Number refers to a binary string that satisfies the following conditions:

  • It is composed only of 0s and 1s.
  • There cannot be consecutive digits ‘1’. In other words, the substring ’11’ must not exist.
  • The string must start and end with 0.

For example, ‘010’, ‘0010’, and ‘1000’ are Binary Friend Numbers. In contrast, ’11’, ‘110’, ‘0110’, and ‘0001’ are not Binary Friend Numbers.

2. Problem Definition of Binary Friend Numbers

Given a natural number n, we define the problem of finding the count of Binary Friend Numbers of length n. This problem can be efficiently solved using Dynamic Programming.

3. Problem Examples

For example, the Binary Friend Numbers of length 1 are ‘0’ and ‘1’, totaling 2. However, the Binary Friend Numbers of length 2 are ’00’, ’01’, and ’10’, totaling 3. The Binary Friend Numbers of length 3 are ‘000’, ‘001’, ‘010’, ‘100’, and ‘101’, totaling 5, while for length 4, they are ‘0000’, ‘0001’, ‘0010’, ‘0100’, ‘0101’, ‘1000’, ‘1001’, and ‘1010’, totaling 8. One can find patterns in this manner.

4. Approach to the Problem

This problem has the following recursive properties:

  • A Binary Friend Number of length n can be derived from Binary Friend Numbers of length n-1 and has two cases: one that ends with 0 and one that ends with 1.
  • Thus, it can be expressed as dp[n] = dp[n-1] + dp[n-2].

5. Implementation Using Dynamic Programming

Now, based on the above relationship, let’s write a code to compute Binary Friend Numbers in Swift. Below is an example of Swift code:

            
                func countBinaryFriends(n: Int) -> Int {
                    guard n > 1 else { return n }
                    
                    var dp = [Int](repeating: 0, count: n + 1)
                    dp[1] = 2 // 0, 1
                    dp[2] = 3 // 00, 01, 10
                    
                    for i in 3...n {
                        dp[i] = dp[i - 1] + dp[i - 2]
                    }
                    
                    return dp[n]
                }

                let n = 4 // Example input
                print(countBinaryFriends(n: n)) // Output the count of Binary Friend Numbers
            
        

6. Time Complexity and Space Complexity

The above algorithm has a time complexity of O(n) concerning n. Additionally, since it utilizes a dp array, it has a space complexity of O(n). If optimized, the space complexity can be reduced to O(1) by only remembering the two previous values:

            
                func optimizedCountBinaryFriends(n: Int) -> Int {
                    guard n > 1 else { return n }
                    
                    var prev1 = 2 // dp[1]
                    var prev2 = 3 // dp[2]
                    var current = 0

                    for i in 3...n {
                        current = prev1 + prev2
                        prev1 = prev2
                        prev2 = current
                    }
                    
                    return current
                }

                let n = 4 // Example input
                print(optimizedCountBinaryFriends(n: n)) // Output the count of optimized Binary Friend Numbers
            
        

7. Conclusion

Through the above process, we were able to solve the problem of finding Binary Friend Numbers. This problem serves as a good example for understanding the basics of Dynamic Programming. I hope you understand and remember the patterns that occur during the process of finding Binary Friend Numbers, as this will help in solving similar problems.

8. Additional Learning Materials

Additionally, it is important to review and practice problems related to algorithms and data structures. Below are recommended resources: