LeetCode 2108: Find First Palindromic String in the Array Solution

Master LeetCode problem 2108 (Find First Palindromic String in the Array), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2108. Find First Palindromic String in the Array

Problem Explanation

Explanation

To solve this problem, we can iterate through the array of strings and for each string, check if it is a palindrome. We can create a helper function to check if a string is a palindrome by comparing characters from the beginning and end of the string. If we find a palindrome string, we return it immediately. If we finish iterating through all strings and do not find any palindrome, we return an empty string.

  • Time complexity: O(N * M) where N is the number of strings in the array and M is the average length of the strings.
  • Space complexity: O(1) as we are not using any extra space other than a few variables.

Solution Code

class Solution {
    public String findFirstPalindromicString(String[] words) {
        for (String word : words) {
            if (isPalindrome(word)) {
                return word;
            }
        }
        return "";
    }
    
    private boolean isPalindrome(String s) {
        int left = 0, right = s.length() - 1;
        while (left < right) {
            if (s.charAt(left) != s.charAt(right)) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2108 (Find First Palindromic String in the Array)?

This page provides optimized solutions for LeetCode problem 2108 (Find First Palindromic String in the Array) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2108 (Find First Palindromic String in the Array)?

The time complexity for LeetCode 2108 (Find First Palindromic String in the Array) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2108 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2108 in Java, C++, or Python.

Back to LeetCode Solutions