find-unique-binary-string.dart (887B)
1 //Given a list of strings (nums) return any combination 2 //of 1's and 0's that does not appear in the nums list. 3 4 //To solve this I used DfS because it is the best way to 5 //find the first solution to a problem (not necessarily 6 //the shortest solution though, but here that does not matter). 7 8 //Time: 260ms Beats: 100% 9 //Memory: 141.5MB Beats: 100% 10 class Solution { 11 String findDifferentBinaryString(List<String> nums) { 12 Set<String> vals = nums.toSet(); 13 return findUnique(vals,'', nums[0].length); 14 } 15 String findUnique(Set<String> vals, String current, int len){ 16 if(current.length == len){ 17 if(vals.contains(current) == false){ 18 return current; 19 } 20 return ''; 21 } 22 String one = findUnique(vals, current + '1', len); 23 if(one.length != 0){ 24 return one; 25 } 26 String zero = findUnique(vals, current + '0', len); 27 return zero; 28 } 29 }