[LeetCode]#2108. Find First Palindromic String in the Array

Fatboy Slim
Dec 22, 2021

--

Environment: Python 3.8

Key technique: for, [::-1]

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.

Analysis:

  1. Reverse input string
  2. If string == reversed string, return it.
  3. If no string and reversed string is equal, return ‘’

Solution:

class Solution:
def firstPalindrome(self, words):
for i in words:
if i==i[::-1]:
return i
return ''

Submissions:

Reference:

https://leetcode.com/problems/find-first-palindromic-string-in-the-array/discuss/1639462/Python-99-faster-Simple-Solution

--

--

Fatboy Slim
Fatboy Slim

Written by Fatboy Slim

Interesting in any computer science.

No responses yet