Invalid Transactions LeetCode solution in C++

1
class Solution {
2
public:
3
/*
4
this question is absolutely frustrating 🙂
5
*/
6

7
/*
8
method to split string
9
*/
10
vector<string> split(string s) {
11
string t = "";
12
vector<string> v;
13
for (int i = 0; i < s.length(); i++) {
14
if (s[i] == ',') {
15
v.push_back(t);
16
t = "";
17
} else {
18
t += s[i];
19
}
20
}
21
v.emplace_back(t);
22
return v;
23
}
24

25
/*
26
this function checks the second condition.
27
*/
28
bool check(string s, string time, string city, map<string, vector<string>> mp) {
29
for (auto x : mp[s]) {
30
vector<string> out = split(x);
31
int val1 = stoi(out[1]);
32
int val2 = stoi(time);
33
if (out[3] != city and abs(val2 - val1) <= 60) {
34
return true;
35
}
36
}
37
return false;
38
}
39

40
vector<string> invalidTransactions(vector<string> &transactions) {
41
vector<string> res; /* stores the result */
42

43
map<string, vector<string>> mp; /* to search for only that person*/
44

45
int n = transactions.size();
46

47
vector<vector<string>> v(n, vector<string>(4)); /*ith v stores info about ith transaction*/
48
int c = 0;
49

50
for (auto x : transactions) {
51
vector<string> out = split(x);
52
v[c] = out;
53
mp[out[0]].emplace_back(x);
54
c += 1;
55
}
56

57
for (int i = 0; i < n; i++) {
58
if (stoi(v[i][2]) > 1000) {
59
/*
60
check for first condition
61
*/
62
res.emplace_back(transactions[i]);
63
} else {
64
/*
65
checks for second condition.
66
*/
67
if (check(v[i][0], v[i][1], v[i][3], mp)) {
68
res.emplace_back(transactions[i]);
69
}
70
}
71
}
72

73
return res;
74
}
75
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0