<p>Given a collection of candidate numbers (<code>candidates</code>) and a target number (<code>target</code>), find all unique combinations in <code>candidates</code>&nbsp;where the candidate numbers sum to <code>target</code>.</p>

<p>Each number in <code>candidates</code>&nbsp;may only be used <strong>once</strong> in the combination.</p>

<p><strong>Note:</strong>&nbsp;The solution set must not contain duplicate combinations.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> candidates = [10,1,2,7,6,1,5], target = 8
<strong>Output:</strong> 
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> candidates = [2,5,2,1,2], target = 5
<strong>Output:</strong> 
[
[1,2,2],
[5]
]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;=&nbsp;candidates.length &lt;= 100</code></li>
	<li><code>1 &lt;=&nbsp;candidates[i] &lt;= 50</code></li>
	<li><code>1 &lt;= target &lt;= 30</code></li>
</ul>

class Solution {
public:
    unordered_set<string> found;

    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        vector<vector<int>> result;
        unordered_set<string> visited;

        found.clear();
        std::sort(candidates.begin(), candidates.end());
        keep_adding(candidates, {}, 0, 0, target, &result, &visited);
           visited.clear();
        return result;
    }
    void keep_adding(const vector<int>& candidates, vector<int> numbers, int sum, int index, int target, vector<vector<int>>* result, unordered_set<string>* visited) {
        string key = "";
        for (const auto n : numbers) {
            key.append(to_string(n)).append("_");
        }
        string key2 = key;
        if (index < candidates.size())
            key2.append(to_string(candidates[index]));
        else
            key2.append("max");

        bool contains = visited->contains(key2);
        // cout << "key2=" << key2 << " contained=" << contains << endl;
        if (contains) return;
        visited->insert(key2);

        int diff = target - sum;
        if (diff < 0) return;
        else if (diff == 0) {
            if (!found.contains(key)) {
                result->push_back(numbers);
                found.insert(key);
            }
            return;
        }
        if (index >= candidates.size()) return;

        auto iter_ub = std::upper_bound(candidates.begin(), candidates.end(), diff);
        auto ub_index = iter_ub - candidates.begin();
        // cout << "sum=" << sum << " index=" << index << " diff=" << diff <<  " upper_bound=" << (ub_index) << endl;
        for (int i = index; i <= ub_index && i < candidates.size(); i++) {
            // cout << "trying index=" << i  << " value=" << candidates[i] << endl;
            numbers.push_back(candidates[i]);
            keep_adding(candidates, numbers, sum + numbers.back(), i+1, target, result, visited);
            numbers.pop_back();
        }
    }
};
20933