在给定数组中查找最后一个回文字符串
给定一个大小为N的字符串数组arr[] ,其中每个字符串仅由小写英文字母组成。任务是返回数组中的最后一个回文字符串。
注意:它保证始终存在一个回文字符串。
例子:
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”
方法:解决方案基于贪婪方法。从最后一个数组的每个字符串检查它是否是回文,并跟踪最后一个回文字符串。请按照以下步骤解决问题:
- 将字符串变量ans初始化为空字符串。
- 使用变量i遍历范围(N, 0]并执行以下任务:
- 如果arr[i]是回文,则将ans的值设置为arr[i] 。
- 执行上述步骤后,打印ans的值作为答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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 last Palindrome string
string LastPalindrome(string arr[], int N)
{
// Loop to find the last palindrome string
for (int i = N - 1; i >= 0; i--) {
// Checking if given string is
// palindrome or not
if (isPalindrome(arr[i])) {
// Return the answer
return arr[i];
}
}
}
// Driver Code
int main()
{
string arr[]
= { "abc", "car", "ada", "racecar",
"cool" };
int N = sizeof(arr) / sizeof(arr[0]);
// Print required answer
cout << LastPalindrome(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 s)
{
// Copy string s char into string a
String a = s;
a = new StringBuffer(a).reverse().toString();
// Check if two string are equal or not
return s.equals(a);
}
// Function to return last Palindrome string
static String LastPalindrome(String arr[], int N) {
// Loop to find the last palindrome string
for (int i = N - 1; i >= 0; i--) {
// Checking if given string is
// palindrome or not
if (isPalindrome(arr[i])) {
// Return the answer
return arr[i];
}
}
return "Hi";
}
// Driver Code
public static void main(String args[]) {
String arr[] = { "abc", "car", "ada", "racecar", "cool" };
int N = arr.length;
// Print required answer
System.out.println(LastPalindrome(arr, N));
}
}
// This code is contributed by saurabh_jaiswal.
Python3
# Python code for the above approach
# Function to check if given string
# is Palindrome or not
def isPalindrome(s):
# find the length of a string
_len = len(s)
for i in range(_len // 2):
# check if first and last string are same
if s[i] != s[_len - 1 - i]:
return 0
return 1
# Function to return last Palindrome string
def LastPalindrome(arr, N):
# Loop to find the last palindrome string
for i in range(N - 1, 0, -1):
# Checking if given string is
# palindrome or not
if isPalindrome(arr[i]):
# Return the answer
return arr[i]
# Driver Code
arr = ["abc", "car", "ada", "racecar", "cool"]
N = len(arr)
# Print required answer
print(LastPalindrome(arr, N))
# This code is contributed by gfgking
C#
// C# program for the above approach
using System;
class GFG {
// Function to check if given string
// is Palindrome or not
static bool isPalindrome(string s)
{
// Copy string s char into string a
char[] a = s.ToCharArray();
Array.Reverse(a);
string p = new string(a);
//a = new StringBuffer(a).reverse().toString();
// Check if two string are equal or not
return s.Equals(p);
}
// Function to return last Palindrome string
static string LastPalindrome(string[] arr, int N) {
// Loop to find the last palindrome string
for (int i = N - 1; i >= 0; i--) {
// Checking if given string is
// palindrome or not
if (isPalindrome(arr[i])) {
// Return the answer
return arr[i];
}
}
return "Hi";
}
// Driver Code
public static void Main() {
string []arr = { "abc", "car", "ada", "racecar", "cool" };
int N = arr.Length;
// Print required answer
Console.Write(LastPalindrome(arr, N));
}
}
// This code is contributed by ukasp.
Javascript
输出:
racecar
时间复杂度: O(N*W) 其中 W 是 arr[] 中任何字符串的最大大小
辅助空间: O(1)