LeetCode #2108 Find First Palindromic String in the Array Solution & Explanation

LeetCode 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.

💡Hint 1

Iterate through the elements in order. As soon as the current element is a palindrome, return it.

💡Hint 2

To check if an element is a palindrome, can you reverse the string?


Solution

When we encounter this question, we know we can refer to LeetCode #9 Palindrome Number.

Previous questions have already discussed two programming solutions for determining whether a string is a palindrome.

C# Solution

Solution1 – reverse the string

public class Solution {
    public string FirstPalindrome(string[] words) {
        foreach(var word in words){
            if(IsPalindrome(word)){
                return word;
            }
        }
        return string.Empty;
    }

    public bool IsPalindrome(string w) {
        char[] charArr = w.ToCharArray();   //the original target
        char[] reverseArr = w.ToCharArray();   

        Array.Reverse(reverseArr);                 
     
        return charArr.SequenceEqual(reverseArr); //compare two array                   
    }
}

Solution2 – for loop

public class Solution {
    public string FirstPalindrome(string[] words) {
        foreach(var word in words){
            if(IsPalindrome(word)){
                return word;
            }
        }
        return string.Empty;
    }

    public bool IsPalindrome(string w) {    
        
        for(int i=0;i<w.Length/2;i++)   
        {
            if(w[i] != w[w.Length-1-i]) 
            {
                return false;           // if any char not the same, return false
            }
        }
        return true;                    
    }
}

Java Solution

Solution1 – reverse the string

class Solution {
    public String firstPalindrome(String[] words) {
        for(String word : words){
            if(isPalindrome(word)){
                return word;
            }
        }
        return "";
    }

    public boolean isPalindrome(String w) {
        StringBuilder str = new StringBuilder(w);
        return w.equals(str.reverse().toString());
    }
}

Solution2 – for loop

class Solution {
    public String firstPalindrome(String[] words) {
        for(String word : words){
            if(isPalindrome(word)){
                return word;
            }
        }
        return "";
    }

    public boolean isPalindrome(String w) {
        for(int i=0;i<w.length()/2;i++)
        {
            if(w.charAt(i) != w.charAt(w.length()-1-i))
            {
                return false;
            }
        }
        return true;
    }
}

Python3 Solution

Solution1 – reverse the string

class Solution:
    def isPalindrome(self, x: str) -> bool:
        return x[::-1] == x

    def firstPalindrome(self, words: List[str]) -> str:
        for word in words:
            if(self.isPalindrome(word)):
                return word
        return ""

Solution2

class Solution:
    def isPalindrome(self, x: str) -> bool:
        l = int(len(x)/2)
        
        for i in range(l):
            if(x[i] != x[-i-1]):
                return False
        return True

    def firstPalindrome(self, words: List[str]) -> str:
        for word in words:
            if(self.isPalindrome(word)):
                return word
        return ""

Conclusion

This question is simply a variant of determining a Palindrome Number, where instead of checking one, we check multiple within a for loop.

🧡If my solution helps, that is my honor!

🧡You can support me by sharing my posts, thanks a lot

If you got any problem about the explanation, please feel free to let me know

The problem link: Find First Palindromic String in the Array – LeetCode

Similar Questions

Some Random LeetCode posts

Leave a Reply

Your email address will not be published. Required fields are marked *