From 713baba7879cd6fedb0ed9cf28fb023dd195afb4 Mon Sep 17 00:00:00 2001 From: Eat-Swap Date: Sun, 1 May 2022 23:04:42 +0800 Subject: [PATCH] add: 220501 [cpp] --- cpp/2205/220501.cpp | 18 ++++++++++++++++++ cpp/2205/CMakeLists.txt | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 cpp/2205/220501.cpp diff --git a/cpp/2205/220501.cpp b/cpp/2205/220501.cpp new file mode 100644 index 0000000..e73fe52 --- /dev/null +++ b/cpp/2205/220501.cpp @@ -0,0 +1,18 @@ +#include +#include + +/** + * 844. Backspace String Compare + * Given two strings s and t, return true if they are equal when both are typed into empty text editors. '#' means a backspace character. + * Note that after backspacing an empty text, the text will continue empty. + */ + +class Solution { +public: + static bool backspaceCompare(const std::string& s, const std::string& t) { + std::string ss, tt; + std::for_each(s.cbegin(), s.cend(), [&](char c) { if (c == '#') { if (!ss.empty()) ss.pop_back(); } else ss.push_back(c); }); + std::for_each(t.cbegin(), t.cend(), [&](char c) { if (c == '#') { if (!tt.empty()) tt.pop_back(); } else tt.push_back(c); }); + return ss == tt; + } +}; diff --git a/cpp/2205/CMakeLists.txt b/cpp/2205/CMakeLists.txt index 29cccfd..efbb028 100644 --- a/cpp/2205/CMakeLists.txt +++ b/cpp/2205/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2205) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2204 220501-CN.cpp) +ADD_EXECUTABLE(2204 220501.cpp)