Leetcode Problem 2108. Find First Palindromic String in the Array

2108. Find First Palindromic String in the Array

Leetcode Solutions

Two Pointers Approach

  • Initialize a function isPalindrome that takes a string as input and returns a boolean indicating whether the string is a palindrome.
  • Inside isPalindrome, use two pointers, left and right, to iterate from the start and end of the string respectively.
  • While left is less than right, compare the characters at these positions.
  • If the characters are not equal, return false.
  • If the characters are equal, increment left and decrement right and continue the loop.
  • If the loop completes without returning false, return true as the string is a palindrome.
  • In the main function, iterate through the array of words.
  • For each word, call isPalindrome.
  • If isPalindrome returns true, immediately return the current word as it is the first palindrome in the array.
  • If no palindromes are found after iterating through the entire array, return an empty string.
UML Thumbnail

Simple Linear Check

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...