Problem Statement
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".
Try here before watching the video.
Video Explanation
Java Code
C++ Code
Python Code
Javascript Code
Go Code
Complexity Analysis
Time Complexity: O(N * K), where N is the length of the words array and K is the length of each word in words.
Space Complexity: O(1)