-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
apply-discount-to-prices.cpp
86 lines (81 loc) · 2.59 KB
/
apply-discount-to-prices.cpp
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Time: O(n)
// Space: O(1)
// string
class Solution {
public:
string discountPrices(string sentence, int discount) {
const auto& format = [&](const auto& x) {
string result;
const auto& integers = to_string(stoll(x) * (100 - discount) / 100);
const auto& decimals = to_string(stoll(x) * (100 - discount) % 100);
const auto& paddings = string(2 - size(decimals), '0');
result += "$";
result += integers;
result += ".";
result += paddings;
result += decimals;
return result;
};
string result;
for (int i = 0, j = -1; i < size(sentence); i = j + 1) {
j = sentence.find(' ', i);
if (j == string::npos) {
j = size(sentence);
}
if (sentence[i] == '$' && j - (i + 1) > 0 && all_of(cbegin(sentence) + i + 1, cbegin(sentence) + j, ::isdigit)) {
result += format(sentence.substr(i + 1, j - (i + 1)));
} else {
for (int k = i; k < j; ++k) {
result.push_back(sentence[k]);
}
}
if (j != size(sentence)) {
result.push_back(' ');
}
}
return result;
}
};
// Time: O(n)
// Space: O(n)
// string
class Solution2 {
public:
string discountPrices(string sentence, int discount) {
const auto& format = [&](const auto& x) {
string result;
const auto& integers = to_string(stoll(x) * (100 - discount) / 100);
const auto& decimals = to_string(stoll(x) * (100 - discount) % 100);
const auto& paddings = string(2 - size(decimals), '0');
result += "$";
result += integers;
result += ".";
result += paddings;
result += decimals;
return result;
};
string result;
for (const auto& x : split(sentence, ' ')) {
if (x[0] == '$' && size(x) - 1 > 0 && all_of(cbegin(x) + 1, cend(x), ::isdigit)) {
result += format(x.substr(1));
} else {
result += x;
}
result.push_back(' ');
}
result.pop_back();
return result;
}
private:
vector<string> split(const string& s, const char delim) {
vector<string> tokens;
stringstream ss(s);
string token;
while (getline(ss, token, delim)) {
if (!empty(token)) {
tokens.emplace_back(token);
}
}
return tokens;
}
};