2108. Find First Palindromic String in the Array

problem

solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
bool isPalindrome(string s){
int l = 0, r = s.size()-1;
while(l<r){
if(s[l++] !=s[r--]) return false;
}
return true;
}
string firstPalindrome(vector<string>& words) {
for(string word:words){
if(isPalindrome(word)) return word;
}
return "";

}
};

analysis

  • time complexity O(n)
  • space complexity O(1)