DEV Community

Tanuja V
Tanuja V

Posted on • Edited on

2 2 1

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 if you found it helpful
Follow for more 🤝 && Happy Coding 🚀

If you enjoy my content, support me by following me on my other socials:
https://linktr.ee/tanujav7

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more