📌  相关文章
📜  给定字符串数组中的回文字符串计数

📅  最后修改于: 2022-05-13 01:57:06.765000             🧑  作者: Mango

给定字符串数组中的回文字符串计数

给定一个大小为N的字符串数组arr[] ,其中每个字符串仅由小写英文字母组成。任务是返回数组中所有回文字符串的计数。

例子:

方法:解决方案基于贪婪方法。检查数组的每个字符串是否为回文,并跟踪计数。请按照以下步骤解决问题:

  • 将计数变量 ans 初始化为 0。
  • 使用变量 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 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



输出
2

时间复杂度: O(N * W) 其中 W 是字符串的平均长度
辅助空间: O(1)