commit 81420085b4cc32b8aed587a69fda849214690fde
parent 1fa352f782e5c1352e7824a927238e06eddd2604
Author: Jack Sangdahl <jack@pngu.org>
Date: Sat, 7 Mar 2026 19:57:51 -0700
1980. find unique binary string
Diffstat:
2 files changed, 22 insertions(+), 19 deletions(-)
diff --git a/find_unique_binary_str.c b/find_unique_binary_str.c
@@ -9,20 +9,26 @@
char *findDifferentBinaryString(char **nums, int numsSize) {
char *ans = (char *)malloc((numsSize + 1) * sizeof(char));
- int j = 0;
for (int i = 0; i < numsSize; i++)
- ans[j++] = nums[i][i] == '0' ? '1' : '0';
- ans[j] = '\0';
- return j ? ans : "";
+ ans[i] = nums[i][i] == '0' ? '1' : '0';
+ ans[numsSize] = '\0';
+ return ans;
}
int main() {
- char *n1[] = {"01", "10"}, *n2[] = {"00", "01"},
- *n3[] = {"111", "011", "001"};
- printf("%s\n",
- findDifferentBinaryString((char **)n1, ARRAY_SIZE(n1))); // expect: 11
- printf("%s\n",
- findDifferentBinaryString((char **)n2, ARRAY_SIZE(n2))); // expect: 11
- printf("%s\n",
- findDifferentBinaryString((char **)n3, ARRAY_SIZE(n3))); // expect: 101
+ char *n1[] = {"01", "10"};
+ char *n2[] = {"00", "01"};
+ char *n3[] = {"111", "011", "001"};
+ char *r1 = findDifferentBinaryString((char **)n1, ARRAY_SIZE(n1));
+ char *r2 = findDifferentBinaryString((char **)n2, ARRAY_SIZE(n2));
+ char *r3 = findDifferentBinaryString((char **)n3, ARRAY_SIZE(n3));
+ printf("%s\n", r1); // expect: 11
+ assert(!strcmp(r1, "11"));
+ printf("%s\n", r2); // expect: 10
+ assert(!strcmp(r2, "10"));
+ printf("%s\n", r3); // expect: 000
+ assert(!strcmp(r3, "000"));
+ free(r1);
+ free(r2);
+ free(r3);
}
diff --git a/find_unique_binary_str.py b/find_unique_binary_str.py
@@ -1,4 +1,5 @@
# 1980. Find Unique Binary String
+from typing import List
"""
given an array of strings 'nums' containing 'n' unique binary strings each of
@@ -7,13 +8,9 @@ length 'n', return a binary string of length 'n' that does not appear in
"""
-class Solution(object):
- def findDifferentBinaryString(self, nums):
- """
- :type nums: List[str]
- :rtype: str
- """
- return "".join(["1" if num[i] == "0" else "0" for i, num in enumerate(nums)])
+class Solution:
+ def findDifferentBinaryString(self, nums: List[str]) -> str:
+ return "".join(["1" if n[i] == "0" else "0" for i, n in enumerate(nums)])
if __name__ == "__main__":