Skip to main content

Command Palette

Search for a command to run...

Find First Palindromic String in the Array

Published
1 min read
Find First Palindromic String in the Array
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

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.

LeetCode Problem - 2108

class Solution {
    public String firstPalindrome(String[] words) {
        // Loop through each word in the array.
        for(int i=0; i<words.length; i++){
            // Check if the current word is a palindrome.
            if(isWordPalindrome(words[i])){
                // If it's a palindrome, return this word.
                return words[i];
            }
        }
        // If no palindrome found, return an empty string.
        return "";
    }

    // Method to check if a word is a palindrome.
    static boolean isWordPalindrome(String str){
        // Get the index of the last character in the word.
        int lastIdx = (str.length())-1;
        // Loop through the characters of the word.
        for(int i=0; i<str.length(); i++){
            // Compare the characters from start and end of the word.
            if(str.charAt(i)!=str.charAt(lastIdx)){
                // If characters don't match, the word is not a palindrome.
                return false;
            }
            // Move to the next character from the end.
            lastIdx--;
        }
        // If all characters match, the word is a palindrome.
        return true;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.