Return the first string in the array that reads the same forward and backward.
Problem
Given an array of strings, scan it from left to right and find the first string that is a palindrome.
A string is a palindrome if it is identical when read from left to right and from right to left. If no string in the array is a palindrome, return an empty string.
Notes
- You must return the earliest palindromic string by index, not the longest one.
- Comparisons are case-sensitive.
- An empty string is considered a palindrome.
Input Format
- A list of strings
words.
Output Format
- Return the first palindromic string in
words. - If none exists, return
"".
Constraints
1 <= words.length- Each
words[i]is a string. - Treat palindromes using exact character matching.
Example 1
Input
words = ["abc","car","ada","racecar","cool"]
Output
"ada"
Explanation
"abc" and "car" are not palindromes. "ada" is the first string that reads the same forward and backward.
Example 2
Input
words = ["not","a","palindrome"]
Output
"a"
Explanation
A single-character string is always a palindrome, so the first palindrome is "a".
Show 1 more example
Example 3
Input
words = ["def","ghi"]
Output
""
Explanation
No string in the array is a palindrome.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.