
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Last Palindrome String in the Given Array
In this problem, we need to find the last palindrome string in the array. If any string is the same in reading, either we start to read from the start or end, we can say the string is a palindrome. We can compare the starting and ending characters to check whether the particular string is palindromic. Another way to find the palindromic string is to reverse the string and compare it with the original string.
Problem statement - We have given an array of length N containing the different strings. We need to find the last palindrome string in the given array.
Sample examples
Input - arr[] = {"werwr", "rwe", "nayan", "tut", "rte"};
Output - ?tut'
Explanation- The ?tut' is the last palindromic string in the given array.
Input - arr[] = {"werwr", "rwe", "nayan", "acd", "sdr"};
Output - ?nayan'
Explanation - The ?nayan' is the last palindrome string in the given array.
Input - arr[] = {"werwr", "rwe", "jh", "er", "rte"};
Output - ""
Explanation - As the array doesn't contain any palindrome string, it prints the empty string.
Approach 1
In this approach, we will start traversing the array from the start and store the last palindromic string in a variable. Also, we will compare the string characters from the start and end to check whether the string is palindromic.
Algorithm
Define the ?lastPal' variable to store the last palindromic string.
Traverse the array.
Use the isPalindrome() function to check whether the string at pth index in the array is a palindrome.
In the isPalindrome() function, use the loop to traverse the string.
Compare the str[i] and str[len - p - 1] characters; if any characters do not match, return false.
Return true once all iteration of the loop completes.
If the current string is a palindrome, update the value of the ?lastPal' variable with the current string.
Return the ?lastPal'.
Example
#include <bits/stdc++.h> using namespace std; bool isPalindrome(string &str) { int size = str.length(); for (int p = 0; p < size / 2; p++) { // compare first ith and last ith character if (str[p] != str[size - p - 1]) { return false; } } return true; } string LastPalindrome(string arr[], int N) { string lastPal = ""; for (int p = 0; p < N; p++) { if (isPalindrome(arr[p])) { // if the current string is palindrome, then update the lastPal string lastPal = arr[p]; } } return lastPal; } int main() { string arr[] = {"werwr", "rwe", "nayan", "abba", "rte"}; int N = sizeof(arr)/sizeof(arr[0]); cout << "The last palindromic string in the given array is " << LastPalindrome(arr, N); return 0; }
Output
The last palindromic string in the given array is abba
Time complexity - O(N*K) as we traverse the array and check for each string whether a string is a palindrome.
Space complexity - O(1) as we use constant space.
Approach 2
In this approach, we will start traversing the array from the last, and when we find the first palindrome string from the last, we will return it. Also, we use the reverse() method to check whether the string is palindrome.
Algorithm
Traverse the array from the last.
Use the isPalindrome() function to check whether the string is palindromic.
In the isPalindrome() function, store the ?str' string in the ?temp' variable.
Use the reverse() method to reverse the temp string.
Return true if str and temp are equal. Otherwise, return false.
If the string at ith index is palindrome, return the string.
Example
#include <bits/stdc++.h> using namespace std; bool isPalindrome(string &str) { string temp = str; reverse(temp.begin(), temp.end()); return str == temp; } string LastPalindrome(string array[], int N) { for (int p = N - 1; p >= 0; p--) { if (isPalindrome(array[p])) { return array[p]; } } // Return a default value if no palindrome is found return "No palindromic string found"; } int main() { string arr[] = {"werwr", "rwe", "nayan", "tut", "rte"}; int N = sizeof(arr) / sizeof(arr[0]); cout << "The last palindromic string in the given array is " << LastPalindrome(arr, N); return 0; }
Output
The last palindromic string in the given array is tut
Time complexity - O(N*K) as we traverse the array and reverse the string.
Space complexity - O(1) as we don't use dynamic space.
Here, we learned two approaches to finding the last palindrome string in the given array. Both approaches' time and space complexity are almost similar, but the second code is more readable and better than the first.
Also, programmers can try to find the second last string in the given array and do more practice.