forked from noisefilter19/LeetCode_Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongestPalindromeSubstring5.java
48 lines (37 loc) · 1.36 KB
/
LongestPalindromeSubstring5.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// The problem was taken from : https://leetcode.com/problems/longest-palindromic-substring/
//
// Created by Ricky Benkovich 13/10/2020
//
public class LongestPalindromeSubstring5 {
public String expandPalindrome(String s, int left, int right, String curr_pali, int i){
while (s.charAt(left) == s.charAt(right)){
if (curr_pali.length() == 0)
if (left == right)
curr_pali = s.charAt(i) + "";
else
curr_pali = s.charAt(left) + "" + s.charAt(right);
else
curr_pali = s.charAt(left) + curr_pali + s.charAt(right);
left --;
right ++;
if (left < 0 || right >= s.length())
break;
}
return curr_pali;
}
public String longestPalindrome(String s) {
String max_Pali = "";
for(int i = 0; i < s.length(); i++) {
String pali_odd= "";
String pali_even = "";
pali_odd = expandPalindrome(s, i, i, pali_odd, i);
if (i+1 < s.length())
pali_even = expandPalindrome(s, i, i+1, pali_even, i);
if (pali_odd.length() > max_Pali.length())
max_Pali = pali_odd;
if (pali_even.length() > max_Pali.length())
max_Pali = pali_even;
}
return max_Pali;
}
}