📌  相关文章
📜  检查字符串包含长度均匀的回文子字符串

📅  最后修改于: 2021-04-27 20:34:10             🧑  作者: Mango

S是仅包含小写英文字母的字符串。我们需要查找是否存在至少一个长度为偶数的回文子字符串。

例子:

Input  : aassss
Output : YES

Input  : gfg
Output : NO

请注意,即使长度的回文必须包含在中间的两个相同的字母。因此,我们只需要检查这种情况。如果在字符串找到两个连续的相同字母,则输出“是”,否则输出“否”。

下面是实现:

C++
// CPP program to check if there is a substring
// palindrome of even length.
#include 
using namespace std;
  
// function to check if two consecutive same 
// characters are present
bool check(string s)
{
    for (int i = 0; i < s.length() - 1; i++)
        if (s[i] == s[i + 1])
            return true;
    return false;
}
  
int main()
{
    string s = "xzyyz";
    if (check(s))
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
    return 0;
}


Java
// Java program to check if there is a substring 
// palindrome of even length. 
  
class GFG {
  
  
// function to check if two consecutive same 
// characters are present 
static boolean check(String s) 
{ 
    for (int i = 0; i < s.length() - 1; i++) 
        if (s.charAt(i) == s.charAt(i+1)) 
            return true; 
    return false; 
} 
  
// Driver Code 
    public static void main(String[] args) {
  
        String s = "xzyyz"; 
    if (check(s)) 
              System.out.println("YES");
    else
        System.out.println("NO");
    }
}


Python3
# Python 3 program to check if there is 
# a substring palindrome of even length.
  
# function to check if two consecutive
# same characters are present
def check(s):
  
    for i in range (0, len(s)):
        if (s[i] == s[i + 1]):
            return True
              
    return False
  
# Driver Code
s = "xzyyz"
if(check(s)):
    print("YES")
else:
    print("NO")
      
# This code is contributed
# by iAyushRAJ


C#
// C# program to check if there is a substring 
// palindrome of even length. 
using System; 
public class GFG {
  
  
// function to check if two consecutive same 
// characters are present 
static bool check(String s) 
{ 
    for (int i = 0; i < s.Length - 1; i++) 
        if (s[i] == s[i+1]) 
            return true; 
    return false; 
} 
  
// Driver Code 
    public static void Main() {
  
        String s = "xzyyz"; 
    if (check(s)) 
              Console.WriteLine("YES");
    else
        Console.WriteLine("NO");
    }
}


PHP


输出:
YES