-
Notifications
You must be signed in to change notification settings - Fork 22
/
LongestPalindromicParenthesesSubstring.java
43 lines (38 loc) · 1.63 KB
/
LongestPalindromicParenthesesSubstring.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
package com.company.amazon;
public class LongestPalindromicParenthesesSubstring {
public static void main(String[] args) {
String input = "((()(()((()))";
System.out.println(getLongestPalindromicSubstring(input));
input = "((()";
System.out.println(getLongestPalindromicSubstring(input));
input = ")()())";
System.out.println(getLongestPalindromicSubstring(input));
input = "()(()))))";
System.out.println(getLongestPalindromicSubstring(input));
}
@SuppressWarnings("Duplicates")
public static int getLongestPalindromicSubstring(String str) {
System.out.println("============================");
char[] input = str.toCharArray();
int longestParenthesesLength = 0;
int startPoint = 0;
for (int i = 1; i < str.length(); i++) {
// For Even Length String
int low = i - 1;
int high = i;
while (low >= 0 && high < input.length && input[low] == '(' && input[high] == ')') {
int currentLongestLength = (high - low) + 1;
if (longestParenthesesLength < currentLongestLength) {
startPoint = low;
longestParenthesesLength = currentLongestLength;
}
low--;
high++;
}
// Odd Length String testing is not required because we want a perfect parentheses which can only be formed
// with Even Length Strings
}
System.out.println(str.substring(startPoint, startPoint + longestParenthesesLength));
return longestParenthesesLength;
}
}