Skip to content

Commit db25d77

Browse files
authored
Create apply-operations-to-make-string-empty.cpp
1 parent a4b59b8 commit db25d77

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
// freq table
5+
class Solution {
6+
public:
7+
string lastNonEmptyString(string s) {
8+
vector<int> cnt(26);
9+
for (const auto& x : s) {
10+
++cnt[x - 'a'];
11+
}
12+
string result;
13+
const int mx = ranges::max(cnt);
14+
for (int i = size(s) - 1; i >= 0; --i) {
15+
if (cnt[s[i] - 'a'] != mx) {
16+
continue;
17+
}
18+
--cnt[s[i] - 'a'];
19+
result.push_back(s[i]);
20+
}
21+
reverse(begin(result), end(result));
22+
return result;
23+
}
24+
};

0 commit comments

Comments
 (0)