Leetcode 2108: Find First Palindromic String in the Array

grid47
grid47
Exploring patterns and algorithms
Apr 10, 2024 4 min read

Given a list of words, return the first word that is a palindrome. A palindrome is a word that reads the same forwards and backwards. If no palindromic word is found, return an empty string.
Problem
Approach
Steps
Complexity
Input: You are given an array of strings, words, where each string consists of lowercase English letters.
Example: words = ["hello", "level", "world"]
Constraints:
• 1 <= words.length <= 100
• 1 <= words[i].length <= 100
• words[i] consists only of lowercase English letters
Output: Return the first palindromic word from the input array. If no such word exists, return an empty string.
Example: For words = ["hello", "level", "world"], the output should be "level".
Constraints:
Goal: The goal is to identify the first word in the array that is palindromic by checking if each word is equal to its reverse.
Steps:
• Iterate through each word in the array.
• For each word, check if it reads the same forward and backward.
• Return the first word that satisfies the condition. If no word satisfies the condition, return an empty string.
Goal: The constraints ensure that the solution must handle up to 100 words, each up to 100 characters long.
Steps:
• The length of the words array is at most 100.
• Each word's length is at most 100 characters.
Assumptions:
• All strings are lowercase English letters.
• The solution must identify palindromes efficiently.
Input: Example 1: words = ["hello", "level", "world"]
Explanation: The first palindromic word is "level", as it is the first word in the list that reads the same forward and backward.

Input: Example 2: words = ["apple", "banana", "radar"]
Explanation: The first palindromic word is "radar".

Input: Example 3: words = ["dog", "cat"]
Explanation: There are no palindromic words in the array, so the output is an empty string.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus