在 C++ 中检查字符串是否包含偶数长度的回文子字符串

假设我们得到一个只包含小写字母的字符串。我们的任务是找出给定字符串中是否存在一个回文长度为偶数的子字符串。如果找到,我们返回 1 否则返回 0。

因此,如果输入类似于“下午”,那么输出将为真。

示例 (C++)

让我们看看以下实现以获得更好的理解 -

#include <bits/stdc++.h>
using namespace std;
bool solve(string string) {
   for (int x = 0; x < string.length() - 1; x++) {
      if (string[x] == string[x + 1])
         return true;
   }
   return false;
}
int main() {
   cout<<solve("afternoon") <<endl;
}

输入

"afternoon"
输出结果
1

猜你喜欢