Find First Palindromic String in the Array

easy array two pointers string

Problem

Given an array of strings words, return the first palindromic string. If no palindromic string exists, return "".

Inputwords = ["abc", "car", "ada", "racecar", "cool"]
Output"ada"
For each word, check with two pointers walking inward. "ada" is the first palindrome.

def first_palindrome(words):
    for w in words:
        l, r = 0, len(w) - 1
        ok = True
        while l < r:
            if w[l] != w[r]:
                ok = False
                break
            l += 1
            r -= 1
        if ok:
            return w
    return ""
function firstPalindrome(words) {
  for (const w of words) {
    let l = 0, r = w.length - 1;
    let ok = true;
    while (l < r) {
      if (w[l] !== w[r]) { ok = false; break; }
      l++;
      r--;
    }
    if (ok) return w;
  }
  return "";
}
class Solution {
    public String firstPalindrome(String[] words) {
        for (String w : words) {
            int l = 0, r = w.length() - 1;
            boolean ok = true;
            while (l < r) {
                if (w.charAt(l) != w.charAt(r)) { ok = false; break; }
                l++;
                r--;
            }
            if (ok) return w;
        }
        return "";
    }
}
string firstPalindrome(vector<string>& words) {
    for (const string& w : words) {
        int l = 0, r = (int) w.size() - 1;
        bool ok = true;
        while (l < r) {
            if (w[l] != w[r]) { ok = false; break; }
            l++;
            r--;
        }
        if (ok) return w;
    }
    return "";
}
Time: O(N · L) Space: O(1)