diff --git a/2231-find-first-palindromic-string-in-the-array/find-first-palindromic-string-in-the-array.js b/2231-find-first-palindromic-string-in-the-array/find-first-palindromic-string-in-the-array.js new file mode 100644 index 0000000..d36fb0f --- /dev/null +++ b/2231-find-first-palindromic-string-in-the-array/find-first-palindromic-string-in-the-array.js @@ -0,0 +1,33 @@ +/** + * @param {string[]} words + * @return {string} + */ +var firstPalindrome = function(words) { + let solution = new Solution(); + return solution.firstPalindrome(words); +}; + +class Solution { + check(s) { + let i = 0, j = s.length - 1; + while (i <= j) { + if (s[i] === s[j]) { + i++; + j--; + } else { + return false; + } + } + return true; + } + + firstPalindrome(words) { + for (let word of words) { + if (this.check(word)) { + return word; + } + } + return ""; + } +} +