Find First Palindromic String in the Array - Leetcode 2108 - Python

preview_player
Показать описание


0:00 - Read the problem
0:16 - Drawing Explanation
3:27 - Coding Explanation

leetcode 2108

#neetcode #leetcode #python
Рекомендации по теме
Комментарии
Автор

Wasn't able to get this one. Was thinking it was a DP/greedy problem. Thanks for posting the solution!

pacolanaco
Автор

Javascript version with 2 pointers:

var firstPalindrome = function (words) {
function isPalindrome(word) {
let L = 0, R = word.length - 1;
while (L < R)
if (word[L++] != word[R--]) return false;
return true;
}
for (let word of words)
if (isPalindrome(word)) return word;
return "";
};

rafael
Автор

isn't the time complexity of this solution still O(n * m)? which is same as using w== w[::-1].

prashanthmangena
Автор

Even though this problem was really easy, I learned that you can give a while loop an else statement as if the while statement was an if in Python! I coded it with my while condition being L <= R, so I needed to add the extra condition to catch when the while condition was false to return the palandromic string. Turns out instead you can just put an else to catch when the while condition becomes false.

rileysikes
Автор

u didnt make a video on this popular question, please make it
30. Substring with Concatenation of All Words

proofhundred
Автор

solved in literally 3 minutes - 1)iterate through the words, construct a StringBuilder and reverse it to check if its a palindrome, O(n)..!! class Solution {
public String firstPalindrome(String[] words) {

for(String i : words){

String res = (new

if(res.equals(i)){
return res;
}

}
return "";
}
}
will now see what my neetyboi did!!

priyamf