Skip to content

Latest commit

 

History

History
102 lines (78 loc) · 2.83 KB

_1980. Find Unique Binary String.md

File metadata and controls

102 lines (78 loc) · 2.83 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 27, 2024

Last updated : February 20, 2025


Related Topics : Array, Hash Table, String, Backtracking

Acceptance Rate : 79.35 %


Solutions

Python

class Solution:
    def findDifferentBinaryString(self, nums: List[str]) -> str:
        return ''.join(['0' if nums[i][i] == '1' else '1' for i in range(len(nums[0]))])
class Solution:
    def findDifferentBinaryString(self, nums: List[str]) -> str:
        trie = {}

        for num in nums :
            curr = trie
            for c in num :
                curr[-1] = curr.get(-1, 0) + 1      # Count cases
                if c not in curr :
                    curr[c] = {}
                curr = curr[c]
        
        output = []

        while len(output) < len(nums[0]) :
            if not trie :
                output.append('0')
            elif len(trie) == 3 :
                zero = trie['0'].get(-1, 0)
                one = trie['1'].get(-1, 0)

                if zero > one :
                    output.append('0')
                    trie = trie.get('0')
                else :
                    output.append('1')
                    trie = trie.get('1')
            elif '0' in trie :
                output.append('1')
                trie = None
            else :
                output.append('0')
                trie = None
                

        return ''.join(output)
class Solution:
    def findDifferentBinaryString(self, nums: List[str]) -> str:
        output = []

        for i in range(len(nums[0])) :
            output.append('0' if nums[i][i] == '1' else '1')
                

        return ''.join(output)

C

char* findDifferentBinaryString(char** nums, int numsSize) {
    char* output = (char*) malloc(sizeof(char) * (numsSize + 1));

    for (int i = 0; i < numsSize; i++) {
        output[i] = (nums[i][i] == '1') ? '0' : '1';
    }
    output[numsSize] = 0;

    return output;
}