<br />
<b>Warning</b>:  Undefined array key "global_protection_id" in <b>/home/wikitechy/public_html/interview-questions/wp-content/plugins/content-protector/inc/class-ps-rest-handler.php</b> on line <b>51</b><br />
{"id":643,"date":"2021-07-13T23:33:34","date_gmt":"2021-07-13T23:33:34","guid":{"rendered":"https:\/\/www.wikitechy.com\/interview-questions\/?p=643"},"modified":"2021-09-11T11:28:24","modified_gmt":"2021-09-11T11:28:24","slug":"how-to-find-the-longest-palindrome-string-in-java","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-find-the-longest-palindrome-string-in-java\/","title":{"rendered":"How to find the Longest Palindrome String in Java ?"},"content":{"rendered":"<div class=\"TextHeading\">\n<div class=\"hddn\">\n<h2 id=\"how-to-find-the-longest-palindrome-string-in-java\" class=\"color-pink\" style=\"text-align: justify;\">How to find the Longest Palindrome String in Java ?<\/h2>\n<\/div>\n<\/div>\n<div class=\"Content\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<ul>\n<li>A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward, example as madam.<\/li>\n<li>Write a java program to find the longest palindrome present in a given string.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div class=\"ImageContent\" style=\"text-align: justify;\">\n<div class=\"hddn\"><img decoding=\"async\" class=\"img-responsive center-block aligncenter\" src=\"https:\/\/cdn.wikitechy.com\/interview-questions\/java\/longest-palindrome-%20in-a-string.png\" alt=\" Longest Palindrome in a String\" \/><\/div>\n<\/div>\n<p style=\"text-align: justify;\" align=\"center\">Longest Palindrome in a String Using Java<\/p>\n<div class=\"text-center row\" style=\"text-align: justify;\">\n<div class=\"col-sm-12\">\n<div id=\"bsa-zone_1590522538159-8_123456\"><\/div>\n<\/div>\n<\/div>\n<div class=\"Content\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<ul>\n<li>The string can be of odd or even length and hence the findLongestPalindromeWithSpecifiedParameter() is called twice for each string.<\/li>\n<li>In the method, findLongestPalindromeWithSpecifiedParameter() left will decrement and move towards the start of the string until it reaches start and then right will increment and move towards the end of the string until it reaches the end.<\/li>\n<li>And any left and right reach destination (i.e. start and end respectively)at any point then the method will return.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div class=\"TextHeading\">\n<div class=\"hddn\">\n<h2 id=\"sample-code\" class=\"color-blue\" style=\"text-align: justify;\">Sample Code<\/h2>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">import java.io.*;<br\/><br\/><br\/>public class LongestPalindromeExample {<br\/><br\/>    public String findTheLongestPalindrome(String str){<br\/>        if (str == null) {<br\/>            return null;<br\/>        }<br\/>        String longestPalindrome = String.valueOf(str.charAt(0));<br\/>        for (int i = 0; i &lt; str.length() - 1; i++) {<br\/>            String returnedPalindrome = findLongestPalindromeWithSpecifiedParameter(str, i, i);<br\/>            if (returnedPalindrome.length() &gt; longestPalindrome.length()) {<br\/>                longestPalindrome = returnedPalindrome;<br\/>            }<br\/>            returnedPalindrome = findLongestPalindromeWithSpecifiedParameter(str, i, i + 1);<br\/>            if (returnedPalindrome.length() &gt; longestPalindrome.length()) {<br\/>                longestPalindrome = returnedPalindrome;<br\/>            }<br\/>        }<br\/>        return longestPalindrome;<br\/>    }<br\/><br\/>    public String findLongestPalindromeWithSpecifiedParameter(String str, int left, int right) {<br\/>        if (left &gt; right)<br\/>            return null;<br\/><br\/>        while (left &gt;= 0 &amp;&amp; right &lt; str.length() &amp;&amp; str.charAt(left) == str.charAt(right)) {<br\/>            left--;<br\/>            right++;<br\/>        }<br\/>        return str.substring(left + 1, right);<br\/>    }<br\/><br\/>    public static void main(String[] args){<br\/>        LongestPalindromeExample longestPalindrome1 = new LongestPalindromeExample();<br\/>        System.out.println(&quot;Longest palindrome in abcmadamcbamadam is &quot; + longestPalindrome1.findTheLongestPalindrome(&quot;abcmadamcbamadam&quot;));<br\/>        System.out.println(&quot;Longest palindrome in abcba is &quot; + longestPalindrome1.findTheLongestPalindrome(&quot;abcba&quot;));<br\/>    }<br\/>}<\/code><\/pre> <\/div>\n<div class=\"TextHeading\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<h2 id=\"output\" class=\"color-blue\">Output<\/h2>\n<\/div>\n<\/div>\n<div class=\"Output\">\n<div class=\"hddn\">\n<figure class=\"highlight\" style=\"text-align: justify;\">\n<pre><code class=\"hljs bash\" data-lang=\"\"><span class=\"nt\">Longest palindrome <span class=\"hljs-keyword\">in<\/span> abcmadamcbamadam is abcmadamcba\r\nLongest palindrome <span class=\"hljs-keyword\">in<\/span> abcba is abcba<\/span><\/code><\/pre>\n<\/figure>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Answer : A palindrome is a word, phrase, number, or other sequence of characters&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"passster_activate_protection":false,"passster_protect_child_pages":"","passster_protection_type":"password","passster_password":"","passster_activate_overwrite_defaults":"","passster_headline":"","passster_instruction":"","passster_placeholder":"","passster_button":"","passster_id":"","passster_activate_misc_settings":"","passster_redirect_url":"","passster_hide":"no","passster_area_shortcode":"","gtb_hide_title":false,"gtb_wrap_title":false,"gtb_class_title":"","gtb_remove_headerfooter":false,"footnotes":""},"categories":[4004],"tags":[195,491,360,203,199,214,198,363,209,15903,4155,205,4157,2936,222,196,212,213,286,4156,4158,4159,207,366,204,4152,217,282,4160,4023,483,206,4153,200,197,280,364,4154,4151,968],"class_list":["post-643","post","type-post","status-publish","format-standard","hentry","category-java","tag-accenture-interview-questions-and-answers","tag-applied-materials-interview-questions-and-answers","tag-atos-interview-questions-and-answers","tag-capgemini-interview-questions-and-answers","tag-casting-networks-india-pvt-limited-interview-questions-and-answers","tag-cgi-group-inc-interview-questions-and-answers","tag-chetu-interview-questions-and-answers","tag-ciena-corporation-interview-questions-and-answers","tag-collabera-technologies-interview-questions-and-answers","tag-craterzone-interview-questions-and-answers","tag-define-palindrome","tag-dell-international-services-india-pvt-ltd-interview-questions-and-answers","tag-find-longest-palindrome-in-a-string-dynamic-programming","tag-fis-global-business-solutions-india-pvt-ltd-interview-questions-and-answers","tag-flipkart-interview-questions-and-answers","tag-ibm-interview-questions-and-answers","tag-indecomm-global-services-interview-questions-and-answers","tag-infosys-technologies-interview-questions-and-answers","tag-lt-infotech-interview-questions-and-answers","tag-longest-palindromic-substring-java","tag-longest-palindromic-substring-on","tag-longest-palindromic-substring-recursion","tag-mphasis-interview-questions-and-answers","tag-netapp-interview-questions-and-answers","tag-oracle-corporation-interview-questions-and-answers","tag-palindrome-string-program-in-java","tag-peoplestrong-interview-questions-and-answers","tag-persistent-systems-interview-questions-and-answers","tag-print-longest-palindromic-subsequence","tag-rbs-india-de-interview-questions-and-answers","tag-reliance-industries-ltd-interview-questions-and-answers","tag-sap-labs-india-pvt-ltd-interview-questions-and-answers","tag-string-palindrome-in-java","tag-tech-mahindra-interview-questions-and-answers","tag-unitedhealth-group-interview-questions-and-answers","tag-virtusa-consulting-services-pvt-ltd-interview-questions-and-answers","tag-wells-fargo-interview-questions-and-answers","tag-what-is-a-palindrome","tag-whats-a-palindrome","tag-wipro-infotech-interview-questions-and-answers"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to find the Longest Palindrome String in Java ? - Wikitechy<\/title>\n<meta name=\"description\" content=\"How to find the Longest Palindrome String in Java ? - A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward, example as madam\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-find-the-longest-palindrome-string-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to find the Longest Palindrome String in Java ? - Wikitechy\" \/>\n<meta property=\"og:description\" content=\"How to find the Longest Palindrome String in Java ? - A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward, example as madam\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-find-the-longest-palindrome-string-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"Wikitechy\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-13T23:33:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-09-11T11:28:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdn.wikitechy.com\/interview-questions\/java\/longest-palindrome-%20in-a-string.png\" \/>\n<meta name=\"author\" content=\"Editor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Editor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-find-the-longest-palindrome-string-in-java\/\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-find-the-longest-palindrome-string-in-java\/\",\"name\":\"How to find the Longest Palindrome String in Java ? - Wikitechy\",\"isPartOf\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-find-the-longest-palindrome-string-in-java\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-find-the-longest-palindrome-string-in-java\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/cdn.wikitechy.com\/interview-questions\/java\/longest-palindrome-%20in-a-string.png\",\"datePublished\":\"2021-07-13T23:33:34+00:00\",\"dateModified\":\"2021-09-11T11:28:24+00:00\",\"author\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757\"},\"description\":\"How to find the Longest Palindrome String in Java ? - A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward, example as madam\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-find-the-longest-palindrome-string-in-java\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-find-the-longest-palindrome-string-in-java\/#primaryimage\",\"url\":\"https:\/\/cdn.wikitechy.com\/interview-questions\/java\/longest-palindrome-%20in-a-string.png\",\"contentUrl\":\"https:\/\/cdn.wikitechy.com\/interview-questions\/java\/longest-palindrome-%20in-a-string.png\"},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/\",\"name\":\"Wikitechy\",\"description\":\"Interview Questions\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757\",\"name\":\"Editor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g\",\"caption\":\"Editor\"},\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/author\/editor\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to find the Longest Palindrome String in Java ? - Wikitechy","description":"How to find the Longest Palindrome String in Java ? - A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward, example as madam","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-find-the-longest-palindrome-string-in-java\/","og_locale":"en_US","og_type":"article","og_title":"How to find the Longest Palindrome String in Java ? - Wikitechy","og_description":"How to find the Longest Palindrome String in Java ? - A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward, example as madam","og_url":"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-find-the-longest-palindrome-string-in-java\/","og_site_name":"Wikitechy","article_published_time":"2021-07-13T23:33:34+00:00","article_modified_time":"2021-09-11T11:28:24+00:00","og_image":[{"url":"https:\/\/cdn.wikitechy.com\/interview-questions\/java\/longest-palindrome-%20in-a-string.png"}],"author":"Editor","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Editor","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-find-the-longest-palindrome-string-in-java\/","url":"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-find-the-longest-palindrome-string-in-java\/","name":"How to find the Longest Palindrome String in Java ? - Wikitechy","isPartOf":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-find-the-longest-palindrome-string-in-java\/#primaryimage"},"image":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-find-the-longest-palindrome-string-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/cdn.wikitechy.com\/interview-questions\/java\/longest-palindrome-%20in-a-string.png","datePublished":"2021-07-13T23:33:34+00:00","dateModified":"2021-09-11T11:28:24+00:00","author":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757"},"description":"How to find the Longest Palindrome String in Java ? - A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward, example as madam","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-find-the-longest-palindrome-string-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-find-the-longest-palindrome-string-in-java\/#primaryimage","url":"https:\/\/cdn.wikitechy.com\/interview-questions\/java\/longest-palindrome-%20in-a-string.png","contentUrl":"https:\/\/cdn.wikitechy.com\/interview-questions\/java\/longest-palindrome-%20in-a-string.png"},{"@type":"WebSite","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website","url":"https:\/\/www.wikitechy.com\/interview-questions\/","name":"Wikitechy","description":"Interview Questions","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757","name":"Editor","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g","caption":"Editor"},"url":"https:\/\/www.wikitechy.com\/interview-questions\/author\/editor\/"}]}},"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/643","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/comments?post=643"}],"version-history":[{"count":2,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/643\/revisions"}],"predecessor-version":[{"id":3416,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/643\/revisions\/3416"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/media?parent=643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/categories?post=643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/tags?post=643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}