#2108. Find First Palindromic String in the Array

ยท

1 min read

https://leetcode.com/problems/find-first-palindromic-string-in-the-array/description/?envType=daily-question&envId=2024-02-13

var firstPalindrome = function (words) {
    function isPalindrome(word) {
        let start = 0, end = word.length - 1

        while(start <= end) {
            if (word[start] !== word[end]) return false

            start++
            end--
        }

        return true
    }

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

    return ""

};

// var firstPalindrome = function (words) {
//     let palindrome

//     for (const word of words) {
//         palindrome = word.split('').reverse().join('')
//         if (palindrome === word) return word
//     }

//     return ""

// };
ย