Find Last Palindrome String in the given Array
Last Updated :
19 Jan, 2022
Given an array of strings arr[] of size N where each string consists only of lowercase English letter. The task is to return the last palindromic string in the array.
Note: It guarantees that always one palindromic string is present.
Examples:
Input: arr[] = {“abc”, “car”, “ada”, “racecar”, “cool”}
Output: “racecar”
Explanation: The Last string that is palindromic is “racecar”.
Note that “ada” is also palindromic, but it is not the Last.
Input: arr[] = {“def”, “aba”}
Output: “aba”
Approach: The solution is based on greedy approach. Check every string of an array from last if it is palindrome or not and also keep track of the last palindrome string. Follow the steps below to solve the problem:
- Initialize a string variable ans as an empty string.
- Iterate over the range (N, 0] using the variable i and perform the following tasks:
- If arr[i] is a palindrome, then set the value of ans as arr[i].
- After performing the above steps, print the value of ans as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPalindrome(string& s)
{
string a = s;
reverse(s.begin(), s.end());
return s == a;
}
string LastPalindrome(string arr[], int N)
{
for ( int i = N - 1; i >= 0; i--) {
if (isPalindrome(arr[i])) {
return arr[i];
}
}
}
int main()
{
string arr[]
= { "abc" , "car" , "ada" , "racecar" ,
"cool" };
int N = sizeof (arr) / sizeof (arr[0]);
cout << LastPalindrome(arr, N);
return 0;
}
|
Java
class GFG {
static boolean isPalindrome(String s)
{
String a = s;
a = new StringBuffer(a).reverse().toString();
return s.equals(a);
}
static String LastPalindrome(String arr[], int N) {
for ( int i = N - 1 ; i >= 0 ; i--) {
if (isPalindrome(arr[i])) {
return arr[i];
}
}
return "Hi" ;
}
public static void main(String args[]) {
String arr[] = { "abc" , "car" , "ada" , "racecar" , "cool" };
int N = arr.length;
System.out.println(LastPalindrome(arr, N));
}
}
|
Python3
def isPalindrome(s):
_len = len (s)
for i in range (_len / / 2 ):
if s[i] ! = s[_len - 1 - i]:
return 0
return 1
def LastPalindrome(arr, N):
for i in range (N - 1 , 0 , - 1 ):
if isPalindrome(arr[i]):
return arr[i]
arr = [ "abc" , "car" , "ada" , "racecar" , "cool" ]
N = len (arr)
print (LastPalindrome(arr, N))
|
C#
using System;
class GFG {
static bool isPalindrome( string s)
{
char [] a = s.ToCharArray();
Array.Reverse(a);
string p = new string (a);
return s.Equals(p);
}
static string LastPalindrome( string [] arr, int N) {
for ( int i = N - 1; i >= 0; i--) {
if (isPalindrome(arr[i])) {
return arr[i];
}
}
return "Hi" ;
}
public static void Main() {
string []arr = { "abc" , "car" , "ada" , "racecar" , "cool" };
int N = arr.Length;
Console.Write(LastPalindrome(arr, N));
}
}
|
Javascript
<script>
function isPalindrome(s)
{
let len = s.length;
for (let i = 0; i < len / 2; i++)
{
if (s[i] !== s[len - 1 - i]) {
return 0;
}
}
return 1;
}
function LastPalindrome(arr, N) {
for (let i = N - 1; i >= 0; i--) {
if (isPalindrome(arr[i])) {
return arr[i];
}
}
}
let arr = [ "abc" , "car" , "ada" , "racecar" ,
"cool" ];
let N = arr.length;
document.write(LastPalindrome(arr, N));
</script>
|
Time Complexity: O(N*W) where W is the maximum size of any string in arr[]
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...