Find Missing Binary String in Array

Leetcode POTD : Find Unique Binary Strings... Given an array that contains n unique binary strings, each of length n, and some possible strings are missing. Target: find a binary string of length n that is not present in the array. Thought process... Initially I thought of generating all possible binary strings of length n. Since the constraint was small, it passed all the test cases. But then I felt generating all 2^n strings just to find one missing is not really optimal, so I started thinking about a better approach 🤔 If we observe the examples carefully, we notice something interesting. There are n strings and each string has length n. Ohh yes, here is the trick 👀 Since the ansmust also be a string of length n, we can construct a new one ourselves. All the given strings are already unique, so if we flip the bit at index i from the i-th string (0 -> 1 or 1 -> 0), the newly formed string will differ from every string at least at one position. That guarantees the string will be unique and missing from the array ✨ Using this idea, the missing binary string can be found in O(n) time. #leetcode #potd #binary #strings #programming #problem_solving #consistency

  • text

To view or add a comment, sign in

Explore content categories