-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8b743cf
commit 29c4c2e
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,57 @@ | ||
class LongestPalindrome { | ||
public String longestPalindrome(String s) { | ||
|
||
ArrayList<String> allPalins= new ArrayList<String>(); | ||
int maxLength = 1; | ||
String palindrome = s.substring(0,1); | ||
for (Character c: s.toCharArray()) { | ||
ListIterator<String> iterator = allPalins.listIterator(); | ||
// for(String substring: allPalins ) { | ||
while(iterator.hasNext()){ | ||
String substring = iterator.next(); | ||
iterator.remove(); | ||
substring = substring+c; | ||
iterator.add(substring); | ||
// allPalins.add(substring); | ||
if(isPalin(substring)) { | ||
if(maxLength<substring.length()) { | ||
maxLength = substring.length(); | ||
palindrome = substring; | ||
} | ||
} | ||
} | ||
allPalins.add(c.toString()); | ||
} | ||
return palindrome; | ||
|
||
} | ||
|
||
public boolean isPalin(String substring) { | ||
return substring.equalsIgnoreCase(new StringBuilder(substring).reverse().toString()); | ||
} | ||
|
||
public String idealSolution(String s) { | ||
if (s == null || s.length() < 1) return ""; | ||
int start = 0, end = 0; | ||
for (int i = 0; i < s.length(); i++) { | ||
int len1 = expandAroundCenter(s, i, i); | ||
int len2 = expandAroundCenter(s, i, i + 1); | ||
int len = Math.max(len1, len2); | ||
if (len > end - start) { | ||
start = i - (len - 1) / 2; | ||
end = i + len / 2; | ||
} | ||
} | ||
return s.substring(start, end + 1); | ||
} | ||
|
||
private int expandAroundCenter(String s, int left, int right) { | ||
int L = left, R = right; | ||
while (L >= 0 && R < s.length() && s.charAt(L) == s.charAt(R)) { | ||
L--; | ||
R++; | ||
} | ||
return R - L - 1; | ||
|
||
} | ||
} |