[LeetCode] Find First Palindromic String in the Array

2108. Find First Palindromic String in the Array

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
string firstPalindrome(vector<string>& words) {
for(auto w : words) {
int i = 0, j = w.length() - 1;
bool ok = true;
while(i < j and ok) {
if(w[i] != w[j]) ok = false;
i += 1, j -= 1;
}
if(ok) return w;
}
return "";
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/02/13/PS/LeetCode/find-first-palindromic-string-in-the-array/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.