-
Notifications
You must be signed in to change notification settings - Fork 20.3k
Added LongestPalindromicSubstring.java to Java/strings #2018
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
0172c29
Added New Java file in Java/Misc
AzadN ee34da4
Update largestRange.java
AzadN 4bee137
Update largestRange.java
AzadN 603e010
Update largestRange.java
AzadN a6f88cd
Update largestRange.java
AzadN 000b7da
Merge pull request #1 from AzadN/Largest_Range
AzadN 9e1c963
Revert "Largest range"
AzadN 42c3aec
Merge pull request #2 from AzadN/revert-1-Largest_Range
AzadN a382936
Added WordBoggle.java
AzadN fc6edff
Merge branch 'master' of https://github.com/AzadN/Java
AzadN cab263e
Update WordBoggle.java
AzadN ffbc3b1
Added RangeInSortedArray.java in Java/Misc
AzadN aeddf97
Merge branch 'master' of https://github.com/AzadN/Java
AzadN 3a6ad5c
Added MinSubstrContainingChars.java in Java/strings
AzadN e72d622
Added new Java file LongestSubstringWithoutDuplication.java
AzadN bf0c6f9
Added GenerateBalancedParenthesis.java
AzadN 5adb4fc
Added New File HappyNumber.java in Java/Maths
AzadN 4819b55
Added Manacher's Algorithm's implementation
AzadN 7660026
Added TopoSortKahn.java to Java/Misc
AzadN 633ec4a
Adding O(n^2) implementation of Longest Palindromic Substring
AzadN 11ac9c8
Adding properly formatted O(n^2) implementation of Longest Palindromi…
AzadN 9eeff4a
Rename longestPalindromicSubstring.java to LongestPalindromicSubstrin…
AzadN File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package strings; | ||
|
||
public class LongestPalindromicSubstring { | ||
|
||
public static String getLPS(String s) { | ||
if (s.length() == 0) return ""; | ||
|
||
// If string has same character, no computations needed. | ||
if (s.replace(String.valueOf(s.charAt(0)), "").length() == 0) return s; | ||
|
||
String current = s.substring(0, 1); | ||
for (int i = 1; i < s.length(); i++) { | ||
// Try to expand the palindrome using the ith character | ||
String odd = getLPSFrom(s, i - 1, i + 1); // Considers centre | ||
String even = getLPSFrom(s, i - 1, i); // Considers no centre | ||
// Comparator<String> strlenComp = (a, b) -> Integer.compare(a.length(), b.length()); | ||
// int longest=strlenComp.compare(odd,even); | ||
String longest = (odd.length() > even.length()) ? odd : even; | ||
if (longest.length() > current.length()) current = longest; | ||
} | ||
return current; | ||
} | ||
|
||
// Check whether a substring starting from left to right index is a palindrome or not | ||
public static String getLPSFrom(String s, int left, int right) { | ||
while (left >= 0 && right < s.length()) { | ||
if (s.charAt(left) != s.charAt(right)) break; | ||
left--; | ||
right++; | ||
} | ||
return s.substring(left + 1, right); | ||
} | ||
|
||
public static void main(String[] args) { | ||
// Testcases | ||
assert getLPS("abcbadefef").equals("abcba"); | ||
assert getLPS("saeedogeeseseegodabsds").equals("dogeeseseegod"); | ||
assert getLPS("uncopyrightable").equals("u"); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add proper problem statement so that it become easy for begginers to understand. As the soul purpose of these implementations is for educational purpose.