DEV Community

Alysa
Alysa

Posted on • Updated on

Find Unique Binary String | LeetCode | Java

class Solution {
    Set<String> numSet;
    int n = 0;
    String res = "";
    public String findDifferentBinaryString(String[] nums) {
        n = nums.length;
        numSet = new HashSet<>();
        for(String s : nums){
            numSet.add(s);
        }

       backTrack("", 0);

        return res;
    }


    void backTrack(String str, int index){
        if(str.length()==n){
            if(!numSet.contains(str)){
                res = str;
            }
            return;
        }

        if(index>n)
            return;

        String str0 = str + "0";
        backTrack(str0, index+1);

        String str1 = str + "1";
        backTrack(str1, index+1);

        return;
    }
}
Enter fullscreen mode Exit fullscreen mode

Thanks for reading๐Ÿฅฐ.
Feel free to comment๐Ÿ–Œ๏ธ and like the post๐Ÿ’“
Follow for more ๐Ÿค && Happy Coding๐Ÿš€๐Ÿ‘ฉโ€๐Ÿ’ป

Don't forget to check-out my other socials :
Github
Hashnode
Twitter(X)

Top comments (0)