Find First Palindromic String in the Array - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: words = ["abc","car","ada","racecar","cool"]
Output: "ada"
💡 Note: The first palindromic string is "ada". Note that "racecar" is also palindromic but comes after "ada".
Example 2 — No Palindrome
$ Input: words = ["notlob","world"]
Output: ""
💡 Note: None of the strings are palindromic, so return empty string.
Example 3 — Single Character
$ Input: words = ["def","ghi","a"]
Output: "a"
💡 Note: Single character strings are palindromes. "a" is the first palindromic string.

Constraints

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

Visualization

Tap to expand
Find First Palindromic String in ArrayInput: ["abc", "car", "ada", "racecar", "cool"]abccaradaracecarcoolNot palindromeNot palindromePalindrome ✓Palindrome but 2ndNot palindromeOutput: "ada"First palindromic string found at index 2
Understanding the Visualization
1
Input Array
Array of strings to check
2
Check Each String
Test palindrome property in order
3
Return First Match
Return first palindromic string found
Key Takeaway
🎯 Key Insight: Check strings sequentially and return the first one that reads the same forwards and backwards
Asked in
Amazon 35 Microsoft 28
24.3K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen