在给定的字符串数组中查找所有回文字符串
给定一个大小为N的字符串数组arr[] ,其中每个字符串仅由小写英文字母组成。任务是查找数组中的所有回文字符串。如果给定数组中不存在回文,则打印 -1。
例子:
Input: arr[] = {“abc”, “car”, “ada”, “racecar”, “cool”}
Output: “ada”, “racecar”
Explanation: These two are the only palindrome strings in the given array
Input: arr[] = {“def”, “ab”}
Output: -1
Explanation: No palindrome string is present in the given array.
方法:解决方案基于贪婪方法。检查数组的每个字符串是否为回文,并跟踪所有回文字符串。请按照以下步骤解决问题:
- 初始化字符串ans的向量。
- 使用变量i遍历[0, N)范围,如果arr[i]是回文,则将其添加到ans中。
- 执行上述步骤后,将存在于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 all Palindrome string
vector PalindromicStrings(string arr[],
int N)
{
vector ans;
// 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.push_back(arr[i]);
}
}
return ans;
}
// Driver Code
int main()
{
string arr[]
= { "abc", "car", "ada", "racecar", "cool" };
int N = sizeof(arr) / sizeof(arr[0]);
// Print required answer
vector s = PalindromicStrings(arr, N);
if(s.size() == 0)
cout << "-1";
for(string st: s)
cout << st << " ";
return 0;
}
Java
// Java code to find the maximum median
// of a sub array having length at least K.
import java.util.*;
public 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 ArrayList PalindromicStrings(String []arr,
int N)
{
ArrayList ans = new ArrayList();
// 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.add(arr[i]);
}
}
return ans;
}
// Driver Code
public static void main(String args[])
{
String []arr
= { "abc", "car", "ada", "racecar", "cool" };
int N = arr.length;
// Print required answer
ArrayList s = PalindromicStrings(arr, N);
if(s.size() == 0)
System.out.print("-1");
for (String st : s)
System.out.print(st + " ");
}
}
// This code is contributed by Samim Hossain Mondal.
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 ArrayList PalindromicStrings(string []arr,
int N)
{
ArrayList ans = new ArrayList();
// 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.Add(arr[i]);
}
}
return ans;
}
// Driver Code
public static void Main()
{
string []arr
= { "abc", "car", "ada", "racecar", "cool" };
int N = arr.Length;
// Print required answer
ArrayList s = PalindromicStrings(arr, N);
if(s.Count == 0)
Console.Write("-1");
foreach(string st in s)
Console.Write(st + " ");
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
ada racecar
时间复杂度: O(N * W) 其中 W 是字符串的平均长度
辅助空间: O(N * W)