Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Number of Ways to Select Buildings

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2222" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

You are given a 0-indexed binary string s which represents the types of buildings along a street where: s[i] = '0' denotes that the ith building is an office and s[i] = '1' denotes that the ith building is a restaurant. As a city official, you would like to select 3 buildings for random inspection. However, to ensure variety, no two consecutive buildings out of the selected buildings can be of the same type. For example, given s = "001101", we cannot select the 1st, 3rd, and 5th buildings as that would form "011" which is not allowed due to having two consecutive buildings of the same type. Return the number of valid ways to select 3 buildings. Example 1: Input: s = "001101" Output: 6 Explanation: The following sets of indices selected are valid: - [0,2,4] from "001101" forms "010" - [0,3,4] from "001101" forms "010" - [1,2,4] from "001101" forms "010" - [1,3,4] from "001101" forms "010" - [2,4,5] from "001101" forms "101" - [3,4,5] from "001101" forms "101" No other selection is valid. Thus, there are 6 total ways. Example 2: Input: s = "11100" Output: 0 Explanation: It can be shown that there are no valid selections. Constraints: 3 <= s.length <= 105 s[i] is either '0' or '1'.

Explanation

Here's the breakdown of the solution:

  • Identify Valid Patterns: The only two valid patterns for selecting three buildings are "010" and "101".
  • Iterate and Count: Iterate through all possible middle building indices. For each middle building, count the number of valid first and third buildings that form the required alternating pattern.
  • Efficient Counting: Instead of re-iterating for each middle building, precompute the counts of '0's and '1's to the left and right of each building.

  • Runtime Complexity: O(n), where n is the length of the string s. Storage Complexity: O(n).

Code

    def count_ways(s):
    n = len(s)
    count = 0

    for j in range(1, n - 1):
        zeros_left = 0
        ones_left = 0
        for i in range(j):
            if s[i] == '0':
                zeros_left += 1
            else:
                ones_left += 1

        zeros_right = 0
        ones_right = 0
        for k in range(j + 1, n):
            if s[k] == '0':
                zeros_right += 1
            else:
                ones_right += 1

        if s[j] == '0':
            count += ones_left * ones_right
        else:
            count += zeros_left * zeros_right

    return count

More from this blog

C

Chatmagic blog

2894 posts