LeetCode Day 22: Cantor's Diagonal Argument

Day 22/30 – LeetCode streak Problem: Find Unique Binary String You are given 'n' distinct binary strings of length 'n' and must return any binary string of length 'n' that doesn’t appear in the array. Core idea (Cantor’s diagonal argument) * Think of 'nums' as an 'n × n' matrix of characters. * Look at the diagonal positions: character 'i' of string 'nums[i]'. * Build a new string where the 'i'-th character is the flip of 'nums[i].charAt(i)' ('0' → '1', '1' → '0'). * This constructed string differs from 'nums[0]' at index '0', from 'nums[1]' at index '1', …, from 'nums[i]' at index 'i', so it cannot be equal to any string in 'nums'. * Time complexity: 'O(n)', space: 'O(n)' for the output string. Day 22 takeaway: This problem is a neat, direct application of Cantor’s diagonalization: by flipping the diagonal bits, you guarantee a new string that is different from every given one in at least one position, without any searching or backtracking. #leetcode #dsa #java #strings #consistency

  • graphical user interface

To view or add a comment, sign in

Explore content categories