forked from lemonbashar/java-algo-expert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindromeSubstring.java
More file actions
68 lines (58 loc) · 1.78 KB
/
PalindromeSubstring.java
File metadata and controls
68 lines (58 loc) · 1.78 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package algoexpert.medium;
/*
PROBLEM:
Given a string find its longest palindromic substring
LOGIC:
at each index check max possible
- 1 center (odd) palindrome
- 2 center (even) palindrome
Solution:
1. Brute Force - time : O(n^3) | space : O(1)
2. Index at Center - time : O(n^2) | space : O(n)
3. Use only index not strings - time : O(n^2) | space: O(1)
*/
public class PalindromeSubstring {
public static void test()
{
System.out.println(longestPalindromicSubstring("abaxyzzyxf"));
}
public static String longestOdd(int index, String str)
{
int first = index;
int second = index;
while (first > -1 && second < str.length() && str.charAt(first) == str.charAt(second))
{
first -= 1;
second += 1;
}
return str.substring(first + 1, second);
}
public static String longestEven(int index, String str)
{
if (index == str.length() - 1) { return ""; }
int first = index;
int second = index + 1;
while (first > -1 && second < str.length() && str.charAt(first) == str.charAt(second))
{
first -= 1;
second += 1;
}
return str.substring(first + 1, second);
}
// time : O(n^2) | space : O(n)
public static String longestPalindromicSubstring(String str)
{
String longestSubstring = "";
for (int i = 0; i < str.length(); ++i )
{
// check 1
String odd = longestOdd(i, str);
// check 2
String even = longestEven(i, str);
String longer = (odd.length() > even.length()) ? odd : even;
if (longer.length() > longestSubstring.length())
{ longestSubstring = longer; }
}
return longestSubstring;
}
}