Skip to main content

Find First Palindromic String in the Array

Problem Description​

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.

Examples​

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".

Complexity Analysis​

*** Time Complexity:** O(1)O(1)

*** Space Complexity:** O(1)O(1)

Constraints​

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

Solution​

Approach​

The approach involves iterating through each word in the list and checking if it is a palindrome by comparing it to its reverse. If a palindrome is found, it is returned immediately. If no palindromes are found after checking all the words, an empty string is returned. This ensures the first palindrome in the list is identified efficiently.

Code in Different Languages​

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

private:
bool isPalindrome(const std::string& word) {
int left = 0;
int right = word.size() - 1;
while (left < right) {
if (word[left] != word[right]) {
return false;
}
left++;
right--;
}
return true;
}
};


References​