|
| 1 | +/** |
| 2 | + * [1980] Find Unique Binary String |
| 3 | + * |
| 4 | + * Given an array of strings nums containing n unique binary strings each of length n, return a binary string of length n that does not appear in nums. If there are multiple answers, you may return any of them. |
| 5 | + * |
| 6 | + * Example 1: |
| 7 | + * |
| 8 | + * Input: nums = ["01","10"] |
| 9 | + * Output: "11" |
| 10 | + * Explanation: "11" does not appear in nums. "00" would also be correct. |
| 11 | + * |
| 12 | + * Example 2: |
| 13 | + * |
| 14 | + * Input: nums = ["00","01"] |
| 15 | + * Output: "11" |
| 16 | + * Explanation: "11" does not appear in nums. "10" would also be correct. |
| 17 | + * |
| 18 | + * Example 3: |
| 19 | + * |
| 20 | + * Input: nums = ["111","011","001"] |
| 21 | + * Output: "101" |
| 22 | + * Explanation: "101" does not appear in nums. "000", "010", "100", and "110" would also be correct. |
| 23 | + * |
| 24 | + * |
| 25 | + * Constraints: |
| 26 | + * |
| 27 | + * n == nums.length |
| 28 | + * 1 <= n <= 16 |
| 29 | + * nums[i].length == n |
| 30 | + * nums[i] is either '0' or '1'. |
| 31 | + * All the strings of nums are unique. |
| 32 | + * |
| 33 | + */ |
| 34 | +pub struct Solution {} |
| 35 | + |
| 36 | +// problem: https://leetcode.com/problems/find-unique-binary-string/ |
| 37 | +// discuss: https://leetcode.com/problems/find-unique-binary-string/discuss/?currentPage=1&orderBy=most_votes&query= |
| 38 | + |
| 39 | +// submission codes start here |
| 40 | + |
| 41 | +impl Solution { |
| 42 | + pub fn find_different_binary_string(nums: Vec<String>) -> String { |
| 43 | + (0..nums.len()) |
| 44 | + .map(|i| (nums[i].as_bytes()[i] ^ 1) as char) |
| 45 | + .collect() |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// submission codes end |
| 50 | + |
| 51 | +#[cfg(test)] |
| 52 | +mod tests { |
| 53 | + use super::*; |
| 54 | + |
| 55 | + #[test] |
| 56 | + fn test_1980_example_1() { |
| 57 | + let nums = vec_string!["01", "10"]; |
| 58 | + |
| 59 | + let result = "11".to_string(); |
| 60 | + |
| 61 | + assert_eq!(Solution::find_different_binary_string(nums), result); |
| 62 | + } |
| 63 | + |
| 64 | + #[test] |
| 65 | + fn test_1980_example_2() { |
| 66 | + let nums = vec_string!["00", "01"]; |
| 67 | + |
| 68 | + let result = "11".to_string(); |
| 69 | + |
| 70 | + assert_eq!(Solution::find_different_binary_string(nums), result); |
| 71 | + } |
| 72 | + |
| 73 | + #[test] |
| 74 | + fn test_1980_example_3() { |
| 75 | + let nums = vec_string!["111", "011", "001"]; |
| 76 | + |
| 77 | + let result = "101".to_string(); |
| 78 | + |
| 79 | + assert_eq!(Solution::find_different_binary_string(nums), result); |
| 80 | + } |
| 81 | +} |
0 commit comments