add: 220501 [cpp]
This commit is contained in:
parent
8e40e5a7bc
commit
713baba787
|
|
@ -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;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
@ -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)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue