Skip to content

Files

Latest commit

9c604ef · Sep 19, 2024

History

History
29 lines (24 loc) · 815 Bytes

Question_1980.md

File metadata and controls

29 lines (24 loc) · 815 Bytes

LeetCode Records - Question 1980 Find Unique Binary String

Attempt 1: Use Integer.parseInt() to convert a string to a binary integer

class Solution {
    public String findDifferentBinaryString(String[] nums) {
        Set<Integer> set = new HashSet<>();

        for (String num : nums) {
            set.add(Integer.parseInt(num, 2));
        }

        int n = (int)Math.pow(2, nums.length);
        for (int i = 0; i < n; i++) {
            if (!set.contains(i)) {
                String val = Integer.toBinaryString(i);
                String format = "%" + nums.length + "s";
                return String.format(format, val).replace(" ", "0");
            }
        }

        return "";
    }
}
  • Runtime: 10 ms (Beats: 23.96%)
  • Memory: 42.32 MB (Beats: 22.78%)