Reach Binary Patterns by Sorting Subsequences

Quick Overview

Determine which wildcard binary patterns can be reached independently from a source string by sorting chosen subsequences while preserving digit counts.

Reach Binary Patterns by Sorting Subsequences

Company: BlackRock

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

# Reach Binary Patterns by Sorting Subsequences You are given a binary string `s` and an array of strings `strs`, each of the same length as `s`. Every target pattern contains only `'0'`, `'1'` and `'?'`; replace each `'?'` independently with either binary digit. You may transform `s` by repeating this operation any number of times, including zero: 1. Choose a subsequence of positions in the current string. 2. Sort the characters at those positions in nondecreasing order, with `'0'` before `'1'`. 3. Put the sorted characters back at the chosen positions in their original positional order. Other positions stay unchanged. Return a boolean array in target order. Its entry is `true` exactly when some completion of that target pattern can be reached from `s`. ### Function `vector<bool> transformStr(string s, vector<string>& strs)` ### Example 1 **Input:** `s = "0101", strs = ["?1?1", "10??", "0??1", "????"]` **Output:** `[true, false, true, true]` **Explanation:** - Complete `"?1?1"` as `"0101"` and use no operations. - Every completion of `"10??"` starts with `1`. The original leading `0` cannot become `1` through the allowed sorting operations. - Complete `"0??1"` as `"0011"`. Sorting the subsequence at indices `[1, 2]`, which contains `"10"`, produces `"01"` and reaches it. - Complete `"????"` as `"0011"` and use the same operation. ### Example 2 **Input:** `s = "000", strs = ["0?0", "?1?", "???"]` **Output:** `[true, false, true]` **Explanation:** Complete the first and third targets as `"000"`. The second target has a fixed `1`, but sorting subsequences cannot introduce a `1` when none exists. ### Constraints - `1 <= n == s.length <= 2000` - Every character of `s` is `'0'` or `'1'`. - `1 <= strs.length <= 2000` - Every string in `strs` has length `n` and contains only `'0'`, `'1'` and `'?'`. ### Clarifications - A subsequence need not be contiguous; its chosen positions keep their order. - Each target is checked independently, starting from the original `s`. - Sorting preserves the total number of zeros and ones. - Only existence is requested; do not return completed patterns or operation sequences. ### What a Strong Answer Covers - Respecting the direction in which binary values can move under sorting. - Assigning wildcards without changing the required total number of each digit. - Checking every target independently at the full combined input size.

Overview: Determine which wildcard binary patterns can be reached independently from a source string by sorting chosen subsequences while preserving digit counts.

BlackRock logo
BlackRock
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Reach Binary Patterns by Sorting Subsequences

You are given a binary string s and an array of strings strs, each of the same length as s. Every target pattern contains only '0', '1' and '?'; replace each '?' independently with either binary digit.

You may transform s by repeating this operation any number of times, including zero:

  1. Choose a subsequence of positions in the current string.
  2. Sort the characters at those positions in nondecreasing order, with '0' before '1' .
  3. Put the sorted characters back at the chosen positions in their original positional order. Other positions stay unchanged.

Return a boolean array in target order. Its entry is true exactly when some completion of that target pattern can be reached from s.

Function

vector<bool> transformStr(string s, vector<string>& strs)

Example 1

Input: s = "0101", strs = ["?1?1", "10??", "0??1", "????"]

Output: [true, false, true, true]

Explanation:

  • Complete "?1?1" as "0101" and use no operations.
  • Every completion of "10??" starts with 1 . The original leading 0 cannot become 1 through the allowed sorting operations.
  • Complete "0??1" as "0011" . Sorting the subsequence at indices [1, 2] , which contains "10" , produces "01" and reaches it.
  • Complete "????" as "0011" and use the same operation.

Example 2

Input: s = "000", strs = ["0?0", "?1?", "???"]

Output: [true, false, true]

Explanation: Complete the first and third targets as "000". The second target has a fixed 1, but sorting subsequences cannot introduce a 1 when none exists.

Constraints

  • 1 <= n == s.length <= 2000
  • Every character of s is '0' or '1' .
  • 1 <= strs.length <= 2000
  • Every string in strs has length n and contains only '0' , '1' and '?' .

Clarifications

  • A subsequence need not be contiguous; its chosen positions keep their order.
  • Each target is checked independently, starting from the original s .
  • Sorting preserves the total number of zeros and ones.
  • Only existence is requested; do not return completed patterns or operation sequences.

What a Strong Answer Covers Guidance

  • Respecting the direction in which binary values can move under sorting.
  • Assigning wildcards without changing the required total number of each digit.
  • Checking every target independently at the full combined input size.