add: 220501 [cpp]

This commit is contained in:
Eat-Swap 2022-05-01 23:04:42 +08:00
parent 8e40e5a7bc
commit 713baba787
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 19 additions and 1 deletions

18
cpp/2205/220501.cpp Normal file
View File

@ -0,0 +1,18 @@
#include <string>
#include <algorithm>
/**
* 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;
}
};

View File

@ -3,4 +3,4 @@ PROJECT(2205)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2204 220501-CN.cpp) ADD_EXECUTABLE(2204 220501.cpp)