-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimumDeletionsToMakeStringBalanced.cpp
More file actions
56 lines (50 loc) · 1.21 KB
/
minimumDeletionsToMakeStringBalanced.cpp
File metadata and controls
56 lines (50 loc) · 1.21 KB
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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
// https://leetcode.com/problems/minimum-deletions-to-make-string-balanced/
// a a b a // 1
// pre [0,0,1,1]
// suf [4,3,3,2]
// b b a a a a a b b
// 2
class Solution
{
public:
std::string A = "a";
std::string B = "b";
int minimumDeletions(std::string s)
{
int length = s.size();
// count "b"
std::vector<int> pre(length);
std::string firstStr;
firstStr.push_back(s[0]);
pre[0] = firstStr == B ? 1 : 0;
for (int i = 1; i < length; i += 1)
{
std::string curStr;
curStr.push_back(s[i]);
int count = curStr == B ? 1 : 0;
pre[i] = pre[i - 1] + count;
}
// count "a"
std::vector<int> suf(length);
std::string lastStr;
lastStr.push_back(s[length - 1]);
suf[length - 1] = lastStr == A ? 1 : 0;
for (int i = length - 2; i >= 0; i -= 1)
{
std::string curStr;
curStr.push_back(s[i]);
int count = curStr == A ? 1 : 0;
suf[i] = suf[i + 1] + count;
}
int result = std::min(pre[length - 1], suf[0]);
for (int i = 0; i < length - 1; i += 1)
{
result = std::min(result, pre[i] + suf[i + 1]);
}
return result;
}
};