Skip to content

Commit

Permalink
add: 删除子串后的字符串最小长度
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Jan 10, 2024
1 parent c8697ce commit b55bed5
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@
"abccbaacz",
"abcda",
"abcdd",
"ABFCACDB",
"absbs",
"ACBBD",
"Advapi",
"andymckay",
"antaprezatepzapreanta",
Expand Down Expand Up @@ -195,6 +197,8 @@
"egcfe",
"emibcn",
"esac",
"FCAB",
"FCACDB",
"gcda",
"gcov",
"GGLLGG",
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,10 @@ C++标准库提供的数据结构实在是太多了,参考[C++标准库头文

###

- [删除子串后的字符串最小长度](src/stack/minimum_string_length_after_removing_substrings.cpp) [栈, 字符串, 模拟]

- LeetCode 2696. 删除子串后的字符串最小长度 <https://leetcode.cn/problems/minimum-string-length-after-removing-substrings>

- [队列中可以看到的人数](src/stack/number_of_visible_people_in_a_queue.cpp) [栈, 数组, 单调栈]

- LeetCode 1944. 队列中可以看到的人数 <https://leetcode.cn/problems/number-of-visible-people-in-a-queue>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions src/stack/minimum_string_length_after_removing_substrings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 删除子串后的字符串最小长度
// https://leetcode.cn/problems/minimum-string-length-after-removing-substrings
// INLINE
// ../../images/stack/minimum_string_length_after_removing_substrings.jpeg

#include <headers.hpp>

class Solution {
public:
int minLength(string s) {
vector<int> st;
for (char c : s) {
st.push_back(c);
int m = st.size();
if (m >= 2 && (st[m - 2] == 'A' && st[m - 1] == 'B' ||
st[m - 2] == 'C' && st[m - 1] == 'D')) {
st.pop_back();
st.pop_back();
}
}
return st.size();
}
};
2 changes: 1 addition & 1 deletion test/lib/lib_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 执行编译时间:2024-01-09 10:59:06
// 执行编译时间:2024-01-10 21:59:48
#include <gtest/gtest.h>
#include <lib.hpp>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <stack/minimum_string_length_after_removing_substrings.cpp>

TEST(删除子串后的字符串最小长度, minLength) {
Solution solution;
// 示例 1:
// 输入:s = "ABFCACDB"
// 输出:2
// 解释:你可以执行下述操作:
// - 从 "ABFCACDB" 中删除子串 "AB",得到 s = "FCACDB" 。
// - 从 "FCACDB" 中删除子串 "CD",得到 s = "FCAB" 。
// - 从 "FCAB" 中删除子串 "AB",得到 s = "FC" 。
// 最终字符串的长度为 2 。
// 可以证明 2 是可获得的最小长度。
string s = "ABFCACDB";
EXPECT_EQ(solution.minLength(s), 2);

// 示例 2:
// 输入:s = "ACBBD"
// 输出:5
// 解释:无法执行操作,字符串长度不变。
s = "ACBBD";
EXPECT_EQ(solution.minLength(s), 5);
}

0 comments on commit b55bed5

Please sign in to comment.