-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.cpp
More file actions
35 lines (32 loc) · 1.05 KB
/
1.cpp
File metadata and controls
35 lines (32 loc) · 1.05 KB
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
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <string>
using namespace std;
int solution(vector<int> numbers) {
unordered_map<int, vector<string>> numbersStr;
for (int i = 0; i < numbers.size(); ++i) {
string strNum = to_string(numbers[i]);
numbersStr[strNum.length()].push_back(strNum);
}
int count = 0;
for (const auto& [numsLength, nums] : numbersStr) {
unordered_map<string, int> seen;
unordered_set<string> countedPairs;
for (const string& num : nums) {
for (int i = 0; i < numsLength; ++i) {
string masked = num;
masked[i] = '*';
if (seen.find(masked) != seen.end()) {
string pair = num + "-" + masked;
if (countedPairs.find(pair) == countedPairs.end()) {
count += seen[masked];
countedPairs.insert(pair);
}
}
seen[masked]++;
}
}
}
return count;
}