diff --git a/cpp/2204/220402.cpp b/cpp/2204/220402.cpp new file mode 100644 index 0000000..dc00933 --- /dev/null +++ b/cpp/2204/220402.cpp @@ -0,0 +1,24 @@ +#include + +/** + * 680. Valid Palindrome II + * Given a string s, return true if the s can be palindrome after deleting at most one character from it. + */ + +class Solution { +private: + static bool isPalindrome(const std::string& basicString, int i, int j) { + for (; i < j; ++i, --j) + if (basicString[i] != basicString[j]) + return false; + return true; + } +public: + static bool validPalindrome(const std::string& s) { + for (int i = 0, j = s.size() - 1; i < j; ++i, --j) + if (s[i] != s[j]) + return isPalindrome(s, i + 1, j) || isPalindrome(s, i, j - 1); + return true; + } +}; + diff --git a/cpp/2204/CMakeLists.txt b/cpp/2204/CMakeLists.txt index 67a70c6..810b8e8 100644 --- a/cpp/2204/CMakeLists.txt +++ b/cpp/2204/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2204) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2204 220401.cpp) +ADD_EXECUTABLE(2204 220402.cpp)