2108. Find First Palindromic String in the Array LeetCode Solution

In this guide, you will get 2108. Find First Palindromic String in the Array LeetCode Solution with the best time and space complexity. The solution to Find First Palindromic String in the Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Find First Palindromic String in the Array solution in C++
  4. Find First Palindromic String in the Array solution in Java
  5. Find First Palindromic String in the Array solution in Python
  6. Additional Resources
2108. Find First Palindromic String in the Array LeetCode Solution image

Problem Statement of Find First Palindromic String in the Array

Given an array of strings words, return the first palindromic string in the array. If there is no such string, return an empty string “”.
A string is palindromic if it reads the same forward and backward.

Example 1:

Input: words = [“abc”,”car”,”ada”,”racecar”,”cool”]
Output: “ada”
Explanation: The first string that is palindromic is “ada”.
Note that “racecar” is also palindromic, but it is not the first.

Example 2:

Input: words = [“notapalindrome”,”racecar”]
Output: “racecar”
Explanation: The first and only string that is palindromic is “racecar”.

Example 3:

Input: words = [“def”,”ghi”]
Output: “”
Explanation: There are no palindromic strings, so the empty string is returned.

Constraints:

1 <= words.length <= 100
1 <= words[i].length <= 100
words[i] consists only of lowercase English letters.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(|\texttt{word}|)

2108. Find First Palindromic String in the Array LeetCode Solution in C++

class Solution {
 public:
  string firstPalindrome(vector<string>& words) {
    for (const string& word : words)
      if (isPalindrome(word))
        return word;
    return "";
  }

 private:
  bool isPalindrome(const string& s) {
    int i = 0;
    int j = s.length() - 1;
    while (i < j)
      if (s[i++] != s[j--])
        return false;
    return true;
  }
};
/* code provided by PROGIEZ */

2108. Find First Palindromic String in the Array LeetCode Solution in Java

class Solution {
  public String firstPalindrome(String[] words) {
    for (final String word : words)
      if (isPalindrome(word))
        return word;
    return "";
  }

  private boolean isPalindrome(final String s) {
    int i = 0;
    int j = 0;
    while (i < j)
      if (s.charAt(i++) != s.charAt(j--))
        return false;
    return true;
  }
}
// code provided by PROGIEZ

2108. Find First Palindromic String in the Array LeetCode Solution in Python

class Solution:
  def firstPalindrome(self, words: list[str]) -> str:
    def isPalindrome(s: str) -> bool:
      i = 0
      j = len(s) - 1
      while i < j:
        if s[i] != s[j]:
          return False
        i += 1
        j -= 1
      return True
    return next((word for word in words if isPalindrome(word)), '')
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.