-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2023.cpp
More file actions
23 lines (22 loc) · 729 Bytes
/
2023.cpp
File metadata and controls
23 lines (22 loc) · 729 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
int numOfPairs(vector<string>& nums, string target) {
unordered_map<string, int> mp;
for (auto num : nums) mp[num]++;
int res = 0;
for (int i = 0; i < target.size() - 1; i++) {
string prev = target.substr(0, i + 1);
string next = target.substr(i + 1);
int prevVal, nextVal;
if (mp.find(prev) == mp.end()) continue;
prevVal = mp[prev];
if (mp.find(next) == mp.end()) continue;
nextVal = mp[next];
if (prev == next) {
res += (prevVal * (prevVal - 1));
}
else res += (prevVal * nextVal);
}
return res;
}
};