Ctrl K

An Unused Bit String

Medium
You are given a list codes of n distinct binary strings, each exactly n characters long. Build a code of the same length that is guaranteed not to be on the list. Here is how: count the ones in every string. Find the smallest number k from 0 to n such that no string on the list has exactly k ones (there are n strings but n + 1 possible counts, so such a k always exists). Return the string of k ones followed by n - k zeros.
Example 1
codes=["00", "11"] → '10'

The codes have 0 and 2 ones, and no code has exactly 1. The answer is one 1 followed by zeros: "10".

Example 2
codes=["010", "110", "001"] → '000'

The counts of ones are 1, 2 and 1. Zero ones never occurs, so the answer is "000".

Example 3
codes=["0"] → '1'

Zero ones is taken and one 1 is free: "1".

Constraints
  • 1 ≤ n = len(codes) ≤ 16
  • len(codes[i]) == n
  • Every string consists only of 0 and 1
  • All strings in codes are distinct
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsEvery Rewording of a Sentence MediumEvery Way to Space the Words HardKeypad Words MediumAnagram Buckets MediumWord Traced on a Letter Grid Medium
loading Python…
Input
codes=["00", "11"]
Expected
'10'

Run checks the first 3 tests in your browser. Submit checks all 16 tests on the server, hidden ones included.