-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
1980-find-unique-binary-string.cpp
51 lines (43 loc) · 2.3 KB
/
1980-find-unique-binary-string.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
This class implements a solution to find a binary string that differs from a given list of binary strings.
Approach:
1. Define a backtrack function to explore all possible binary strings of the same length as the input strings.
2. Use a set to store the input strings to efficiently check for duplicates.
3. Backtrack through all possible binary strings of length equal to the input strings.
4. If a binary string is found that does not exist in the set of input strings, update the result and return.
Variables:
- result: Holds the result, initialized to an empty string.
- backtrack(): Recursive function to generate binary strings and check for uniqueness.
- inputStrings: Input vector of binary strings.
- currentString: Current binary string being constructed during backtracking.
- stringLength: Length of binary strings in the input.
- stringSet: Set to store input binary strings for fast duplicate checking.
Time Complexity: O(2^N * N), where N is the length of the binary strings.
- The function 'backtrack' explores all possible binary strings of length N, which is O(2^N).
- Checking for uniqueness in the set takes O(1) time on average.
Space Complexity: O(2^N * N) considering the space required to store the generated binary strings during backtracking.
*/
class Solution {
public:
string result = "";
void backtrack(vector<string>& inputStrings, string& currentString, int stringLength, set<string>& stringSet) {
if (!result.empty()) return;
if (currentString.size() == stringLength && stringSet.find(currentString) == stringSet.end()) {
result = currentString;
return;
}
if (currentString.size() > stringLength) return;
for (char ch = '0'; ch <= '1'; ++ch) {
currentString.push_back(ch);
backtrack(inputStrings, currentString, stringLength, stringSet);
currentString.pop_back();
}
}
string findDifferentBinaryString(vector<string>& inputStrings) {
int stringLength = inputStrings[0].size();
string currentString = "";
set<string> stringSet(inputStrings.begin(), inputStrings.end());
backtrack(inputStrings, currentString, stringLength, stringSet);
return result;
}
};