Java: Given a string, find the longest palindromic contiguous substring.
Given a string, find the longest palindromic
contiguous substring.
If there are more than one with
the maximum length, return any one.
For example, the longest palindromic
substring of "aabcdcb" is "bcdcb".
The longest palindromic
substring of "bananas" is "anana".
Approach
Given String for example: “aabcdcb”
- Let’s split problem in to smaller parts.
- First lets chcek if the given String is valid or not if not, valid let’s
return “Invalid String”
response.
- Next, we can write a helper method that checks if given String is
palindrome or not (I used StringBuffer reverse method to check
the String)
- So, we have 2 utility methods lets concentrate on finding longest palindrome
substring.
Note:
·
The above
solution is not the ultimate or the only way to approach the problem.
·
Attaching
screenshot because you need to practice than copying code and executing in IDE.
·
As I said
there are ‘N’ number of ways to solve the issue, here I tried using inbuilt
methods from String class and get the solution.
·
Comments and
suggestions are always welcome.
·
Git-hub
link: https://github.com/failed-peanut/java
Comments
Post a Comment