{"id":9359,"date":"2021-12-31T15:38:17","date_gmt":"2021-12-31T23:38:17","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9359"},"modified":"2021-12-31T15:40:06","modified_gmt":"2021-12-31T23:40:06","slug":"leetcode-1980-find-unique-binary-string","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/bit\/leetcode-1980-find-unique-binary-string\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1980. Find Unique Binary String"},"content":{"rendered":"\n<p>Given an array of strings&nbsp;<code>nums<\/code>&nbsp;containing&nbsp;<code>n<\/code>&nbsp;<strong>unique<\/strong>&nbsp;binary strings each of length&nbsp;<code>n<\/code>, return&nbsp;<em>a binary string of length&nbsp;<\/em><code>n<\/code><em>&nbsp;that&nbsp;<strong>does not appear<\/strong>&nbsp;in&nbsp;<\/em><code>nums<\/code><em>. If there are multiple answers, you may return&nbsp;<strong>any<\/strong>&nbsp;of them<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [\"01\",\"10\"]\n<strong>Output:<\/strong> \"11\"\n<strong>Explanation:<\/strong> \"11\" does not appear in nums. \"00\" would also be correct.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [\"00\",\"01\"]\n<strong>Output:<\/strong> \"11\"\n<strong>Explanation:<\/strong> \"11\" does not appear in nums. \"10\" would also be correct.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [\"111\",\"011\",\"001\"]\n<strong>Output:<\/strong> \"101\"\n<strong>Explanation:<\/strong> \"101\" does not appear in nums. \"000\", \"010\", \"100\", and \"110\" would also be correct.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == nums.length<\/code><\/li><li><code>1 &lt;= n &lt;= 16<\/code><\/li><li><code>nums[i].length == n<\/code><\/li><li><code>nums[i]&nbsp;<\/code>is either&nbsp;<code>'0'<\/code>&nbsp;or&nbsp;<code>'1'<\/code>.<\/li><li>All the strings of&nbsp;<code>nums<\/code>&nbsp;are&nbsp;<strong>unique<\/strong>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Hashtable<\/strong><\/h2>\n\n\n\n<p>We can use bitset to convert between integer and binary string.<\/p>\n\n\n\n<p>Time complexity: O(n<sup>2<\/sup>)<br>Space complexity: <meta charset=\"utf-8\">O(n<sup>2<\/sup>)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  string findDifferentBinaryString(vector<string>& nums) {\n    const int n = nums.size();\n    unordered_set<int> seen(1 << n);\n    for (const string&#038; num : nums)\n      seen.insert(bitset<16>(num).to_ulong());\n    for (int i = 0; i < 1 << n; ++i)\n      if (!seen.count(i)) return bitset<16>(i).to_string().substr(16 - n);\n    return \"\";\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: One bit a time<\/strong><\/h2>\n\n\n\n<p>Let ans[i] = &#8216;1&#8217; &#8211; nums[i][i], s.t. ans is at least one bit different from any strings.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  string findDifferentBinaryString(vector<string>& nums) {    \n    const int n = nums.size();\n    string ans(n, '0');\n    for (int i = 0; i < n; ++i)\n      ans[i] = '1' - nums[i][i] + '0';\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of strings&nbsp;nums&nbsp;containing&nbsp;n&nbsp;unique&nbsp;binary strings each of length&nbsp;n, return&nbsp;a binary string of length&nbsp;n&nbsp;that&nbsp;does not appear&nbsp;in&nbsp;nums. If there are multiple answers, you may return&nbsp;any&nbsp;of them.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[39,177,4],"class_list":["post-9359","post","type-post","status-publish","format-standard","hentry","category-bit","tag-binary","tag-medium","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9359","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9359"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9359\/revisions"}],"predecessor-version":[{"id":9362,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9359\/revisions\/9362"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9359"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}