-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
55 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
src/stack/minimum_string_length_after_removing_substrings.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
||
|
23 changes: 23 additions & 0 deletions
23
test/stack/minimum_string_length_after_removing_substrings_test.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |