Apply Discount to Prices LeetCode solution in C++

1
class Solution {
2
public:
3
string discountPrices(string sentence, int discount) {
4
// doit is a function
5
auto doit = [&](string word) {
6
int n(size(word));
7
if (word[0] != '$' or n == 1) return word;
8

9
long long price = 0;
10
for (int i = 1; i < n; i++) {
11
if (!isdigit(word[i])) return word;
12
price = price * 10 + (word[i] - '0');
13
}
14

15
stringstream ss2;
16
double discountPercentage = (100 - discount) / 100.0;
17
ss2 << fixed << setprecision(2) << (discountPercentage * price);
18
return "$" + ss2.str();
19
};
20

21
string word, res;
22
stringstream ss(sentence);
23

24
while (ss >> word) {
25
res += doit(word) + " ";
26
}
27

28
res.pop_back();
29
return res;
30
}
31
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0