Dada una array de strings arr[] de tamaño N donde cada string consta solo de letras minúsculas en inglés. La tarea es devolver el recuento de todas las strings palindrómicas en la array.
Ejemplos:
Entrada: arr[] = {“abc”,”car”,”ada”,”racecar”,”cool”}
Salida: 2
Explicación: “ada” y “racecar” son las dos strings palíndromo.Entrada: arr[] = {“def”,”aba”}
Salida: 1
Explicación: “aba” es la única string palíndromo.
Enfoque: La solución se basa en el enfoque codicioso . Verifique cada string de una array si es palíndromo o no y también realice un seguimiento del conteo. Siga los pasos a continuación para resolver el problema:
- Inicialice una variable de conteo como 0.
- Itere sobre el rango [0, N) usando la variable i y si arr[i] es un palíndromo, entonces incremente el valor de ans.
- Después de realizar los pasos anteriores, imprima el valor de ans como respuesta.
A continuación se muestra la implementación del enfoque anterior.
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to check if given string // is Palindrome or not bool isPalindrome(string& s) { // Copy string s char into string a string a = s; reverse(s.begin(), s.end()); // Check if two string are equal or not return s == a; } // Function to return count // of Palindrome string int PalindromicStrings(string arr[], int N) { int ans = 0; // Loop to find palindrome string for (int i = 0; i < N; i++) { // Checking if given string is // palindrome or not if (isPalindrome(arr[i])) { // Update answer variable ans++; } } return ans; } // Driver Code int main() { string arr[] = { "abc", "car", "ada", "racecar", "cool" }; int N = sizeof(arr) / sizeof(arr[0]); // Print required answer cout << PalindromicStrings(arr, N); return 0; }
Java
// java program for the above approach class GFG { // Function to check if given String // is Palindrome or not static boolean isPalindrome(String str) { // Start from leftmost and rightmost corners of str int l = 0; int h = str.length() - 1; // Keep comparing characters while they are same while (h > l) { if (str.charAt(l++) != str.charAt(h--)) { return false; } } return true; } // Function to return all Palindrome String static int PalindromicStrings(String []arr, int N) { int ans = 0; // Loop to find palindrome String for (int i = 0; i < N; i++) { // Checking if given String is // palindrome or not if (isPalindrome(arr[i])) { // Update answer variable ans++; } } return ans; } // Driver Code public static void main(String[] args) { String []arr = { "abc", "car", "ada", "racecar", "cool" }; int N = arr.length; System.out.print(PalindromicStrings(arr, N)); } } // This code is contributed by Rajput-Ji
Python3
# Python program for the above approach # Function to check if given String # is Palindrome or not def isPalindrome(str): # Start from leftmost and rightmost corners of str l = 0; h = len(str) - 1; # Keep comparing characters while they are same while (h > l): if (str[l] != str[h]): return False; l += 1; h -= 1; return True; # Function to return all Palindrome String def PalindromicStrings(arr, N): ans = 0; # Loop to find palindrome String for i in range(N): # Checking if given String is # palindrome or not if (isPalindrome(arr[i])): # Update answer variable ans += 1; return ans; # Driver Code if __name__ == '__main__': arr = ["abc", "car", "ada", "racecar", "cool"]; N = len(arr); print(PalindromicStrings(arr, N)); # This code is contributed by 29AjayKumar
C#
// C# program for the above approach using System; using System.Collections; class GFG { // Function to check if given string // is Palindrome or not static bool isPalindrome(string str) { // Start from leftmost and rightmost corners of str int l = 0; int h = str.Length - 1; // Keep comparing characters while they are same while (h > l) { if (str[l++] != str[h--]) { return false; } } return true; } // Function to return all Palindrome string static int PalindromicStrings(string []arr, int N) { int ans = 0; // Loop to find palindrome string for (int i = 0; i < N; i++) { // Checking if given string is // palindrome or not if (isPalindrome(arr[i])) { // Update answer variable ans++; } } return ans; } // Driver Code public static void Main() { string []arr = { "abc", "car", "ada", "racecar", "cool" }; int N = arr.Length; Console.Write(PalindromicStrings(arr, N)); } } // This code is contributed by Samim Hossain Mondal.
Javascript
<script> // Javascript program for the above approach // Function to check if given string // is Palindrome or not function isPalindrome(s) { // Copy string s char into string a let a = s; s = s.split("").reverse().join(""); // Check if two string are equal or not return s == a; } // Function to return count // of Palindrome string function PalindromicStrings(arr, N) { let ans = 0; // Loop to find palindrome string for (let i = 0; i < N; i++) { // Checking if given string is // palindrome or not if (isPalindrome(arr[i])) { // Update answer variable ans++; } } return ans; } // Driver Code let arr = ["abc", "car", "ada", "racecar", "cool"]; let N = arr.length; // Print required answer document.write(PalindromicStrings(arr, N)); // This code is contributed by saurabh_jaiswal. </script>
2
Complejidad de Tiempo: O(N * W) donde W es la longitud promedio de las strings
Espacio Auxiliar: O(1), ya que no se ha tomado espacio extra.