Skip to main content

Find First Palindromic String in the Array - LeetCode Daily Challenge

Prerna Sharma

Problem Statement

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.

Example 2

Input: words = ["notapalindrome","racecar"]
Output: "racecar"
Explanation: The first and only string that is palindromic is "racecar".

Try here before watching the video.

Video Explanation

Java Code

class Solution {
    private boolean isPalindrome(String word) {
        int left = 0, right = word.length() - 1;
        while(left < right) {
            if(word.charAt(left++) != word.charAt(right--)) {
                return false;
            }
        }
        return true;
    }

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

C++ Code

class Solution {
private:
    bool isPalindrome(string word) {
        int left = 0, right = word.length() - 1;
        while (left < right) {
            if (word[left++] != word[right--]) {
                return false;
            }
        }
        return true;
    }

public:
    string firstPalindrome(vector<string>& words) {
        for (const string& word : words) {
            if (isPalindrome(word)) {
                return word;
            }
        }
        return "";
    }
};

Python Code

class Solution:
    def isPalindrome(self, word: str) -> bool:
        left, right = 0, len(word) - 1
        while left < right:
            if word[left] != word[right]:
                return False
            left += 1
            right -= 1
        return True

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

Javascript Code

var firstPalindrome = function(words) {
    const isPalindrome = word => {
        let left = 0, right = word.length - 1;
        while (left < right) {
            if (word[left++] !== word[right--]) {
                return false;
            }
        }
        return true;
    };

    for (const word of words) {
        if (isPalindrome(word)) {
            return word;
        }
    }
    return "";
};

Go Code

func isPalindrome(word string) bool {
	left, right := 0, len(word)-1
	for left < right {
		if word[left] != word[right] {
			return false
		}
		left++
		right--
	}
	return true
}

func firstPalindrome(words []string) string {
	for _, word := range words {
		if isPalindrome(word) {
			return word
		}
	}
	return ""
}

Complexity Analysis

Time Complexity: O(N * K), where N is the length of the words array and K is the length of each word in words.
Space Complexity: O(1)