From 97b4d62734acf7533a96b08bcc6044eb94847931 Mon Sep 17 00:00:00 2001 From: Justsvetoslavov Date: Thu, 13 Jan 2022 21:15:47 +0200 Subject: Add Pract. solutions. --- pract_SI/Pract. 09/task7.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 pract_SI/Pract. 09/task7.cpp (limited to 'pract_SI/Pract. 09/task7.cpp') diff --git a/pract_SI/Pract. 09/task7.cpp b/pract_SI/Pract. 09/task7.cpp new file mode 100644 index 0000000..5065a6d --- /dev/null +++ b/pract_SI/Pract. 09/task7.cpp @@ -0,0 +1,32 @@ +#include + +bool checkIsPalindrom(const vector& v) +{ + //if (v.size() < 2) - not needed as for-loop covers this case. + // return true; + + for (size_t i = 0; i < v.size() / 2; i++) + if (v[i] != v[v.size() - 1 - i]) + return false; + + return true; +} + +bool isPalindrom(const std::vector& v, int operations) +{ + if(v.empty()) + return false; + + if (operations < 0) + return false; + + if (checkIsPalindrom(v)) + return true; + + std::vector left = v, right = v; + left.erase(left.begin()); //remove first + right.pop_back(); //remove last + operations--; + + return isPalindrom(left, operations) || isPalindrom(right, operations); +} \ No newline at end of file -- cgit v1.3.1-23-gd82d