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)