S es una string que contiene solo alfabetos ingleses en minúsculas. Necesitamos encontrar si existe al menos una substring palindrómica cuya longitud sea par.
Ejemplos:
Input : aassss Output : YES Input : gfg Output : NO
Tenga en cuenta que un palíndromo de longitud uniforme debe contener dos alfabetos iguales en el medio. Así que sólo tenemos que comprobar esta condición. Si encontramos dos alfabetos iguales consecutivos en la string, emitimos «SÍ», de lo contrario, «NO».
A continuación se muestra la implementación:
C++
// C++ program to check if there is a substring // palindrome of even length. #include <bits/stdc++.h> 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
<?php // PHP program to check if there is a // substring palindrome of even length. // function to check if two consecutive // same characters are present function check($s) { for ($i = 0; $i < strlen($s) - 1; $i++) if ($s[$i] == $s[$i + 1]) return true; return false; } // Driver Code $s = "xzyyz"; if (check($s)) echo "YES","\n"; else echo "NO" ,"\n"; // This code is contributed by ajit ?>
Javascript
<script> // Javascript program to check if there is // a substring palindrome of even length. // Function to check if two consecutive same // characters are present function check(s) { for(let i = 0; i < s.length - 1; i++) if (s[i] == s[i + 1]) return true; return false; } // Driver code let s = "xzyyz"; if (check(s)) document.write("YES"); else document.write("NO"); // This code is contributed by suresh07 </script>
Producción:
YES
Publicación traducida automáticamente
Artículo escrito por SujanDutta y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA