Dada una string str , la tarea es verificar si la string dada se puede dividir en dos mitades, cada una de las cuales es palindrómica . Si es posible, escriba Sí . De lo contrario , imprima No.
Ejemplos:
Entrada: str = “naan”
Salida: No
Explicación:
Ya que ambas mitades “na” y “an” no son palíndromos.
Entrada: mamá
Salida: Sí
Explicación:
Dado que la mitad de «mamá» y «papá» son palíndromos.
Enfoque:
siga los pasos a continuación para resolver el problema:
- Iterar sobre los primeros ((N / 2) / 2 – 1) índices de la string.
- Compruebe simultáneamente si ambas mitades son palíndromos o no mediante las siguientes dos condiciones:
- Si S[i] no es igual a S[N/2 – 1 – i] , entonces la primera mitad no es palindrómica.
- Si S[N/2 + i] no es igual a S[N – 1 – i] , entonces la segunda mitad no es palindrómica.
- Si ninguna de las condiciones anteriores se cumple ni una sola vez durante la iteración, ambas mitades son palindrómicas. Imprimir Sí .
- De lo contrario , imprima No.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ Program to check whether // both halves of a string is // Palindrome or not #include <bits/stdc++.h> using namespace std; // Function to check if both halves // of a string are palindrome or not void checkPalindrome(string S) { // Length of string int N = S.size(); // Initialize both part as true bool first_half = true; bool second_half = true; int cnt = (N / 2) - 1; for (int i = 0; i < ((N / 2) / 2); i++) { // If first half is not palindrome if (S[i] != S[cnt]) { first_half = false; break; } // If second half is not palindrome if (S[N / 2 + i] != S[N / 2 + cnt]) { second_half - false; break; } cnt--; } // If both halves are Palindrome if (first_half && second_half) { cout << "Yes" << endl; } else { cout << "No" << endl; } } int main() { string S = "momdad"; checkPalindrome(S); return 0; }
Java
// Java Program to check whether // both halves of a string is // Palindrome or not public class Main { // Function to check and return if // both halves of a string are // palindromic or not public static void checkPalindrome( String S) { // Length of string int N = S.length(); // Initialize both part as true boolean first_half = true; boolean second_half = true; int cnt = (N / 2) - 1; for (int i = 0; i < (N / 2); i++) { // If first half is not palindrome if (S.charAt(i) != S.charAt(cnt)) { first_half = false; break; } // If second half is not palindrome if (S.charAt(N / 2 + i) != S.charAt(N / 2 + cnt)) { second_half = false; break; } cnt--; } // If both halves are Palindrome if (first_half && second_half) { System.out.println("Yes"); } else { System.out.println("No"); } } // Driver Code public static void main(String[] args) { String S = "momdad"; checkPalindrome(S); } }
Python3
# Python3 program to check whether # both halves of a string is # palindrome or not # Function to check if both halves # of a string are palindrome or not def checkPalindrome(S): # Length of string n = len(S) # Initialize both part as true first_half = True second_half = True cnt = (n // 2) - 1 for i in range(0, int((n / 2) / 2)): # If first half is not palindrome if (S[i] != S[cnt]): first_half = False break # If second half is not palindrome if (S[n // 2 + i] != S[n // 2 + cnt]): second_half = False break cnt -= 1 # If both halves are palindrome if (first_half and second_half): print('Yes', end = '') else: print('No', end = '') # Driver code if __name__=='__main__': S = 'momdad' checkPalindrome(S) # This code is contributed by virusbuddah_
C#
// C# program to check whether // both halves of a string is // Palindrome or not using System; class GFG{ // Function to check and return if // both halves of a string are // palindromic or not public static void checkPalindrome(String S) { // Length of string int N = S.Length; // Initialize both part as true bool first_half = true; bool second_half = true; int cnt = (N / 2) - 1; for(int i = 0; i < (N / 2); i++) { // If first half is not palindrome if (S[i] != S[cnt]) { first_half = false; break; } // If second half is not palindrome if (S[N / 2 + i] != S[N / 2 + cnt]) { second_half = false; break; } cnt--; } // If both halves are Palindrome if (first_half && second_half) { Console.Write("Yes"); } else { Console.Write("No"); } } // Driver code public static void Main() { String S = "momdad"; checkPalindrome(S); } } // This code is contributed by grand_master
Javascript
<script> // JavaScript Program to check whether // both halves of a string is // Palindrome or not // Function to check and return if // both halves of a string are // palindromic or not function checkPalindrome( S) { // Length of string var N = S.length; // Initialize both part as true var first_half = true; var second_half = true; var cnt = parseInt((N / 2)) - 1; for (i = 0; i < (N / 2); i++) { // If first half is not palindrome if (S.charAt(i) != S.charAt(cnt)) { first_half = false; break; } // If second half is not palindrome if (S.charAt(N / 2 + i) != S.charAt(N / 2 + cnt)) { second_half = false; break; } cnt--; } // If both halves are Palindrome if (first_half && second_half) { document.write("Yes"); } else { document.write("No"); } } // Driver Code var S = "momdad"; checkPalindrome(S); // This code contributed by aashish1995 </script>
Producción:
Yes
Complejidad temporal: O(N)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por divyeshrabadiya07 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA