This commit is related to BAEL-7722 (#16382)
This commit aims to update SymmetricSubstringMaxLengthUnitTest.java.
This commit is contained in:
parent
1c569bcafc
commit
ae5290fe76
@ -5,73 +5,70 @@ import org.junit.Test;
|
|||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
public class SymmetricSubstringMaxLengthUnitTest {
|
public class SymmetricSubstringMaxLengthUnitTest {
|
||||||
String input = "<><??>>";
|
String input = "abba";
|
||||||
int expected = 4;
|
int expected = 4;
|
||||||
|
|
||||||
@Test
|
static int findLongestSymmetricSubstringUsingSymmetricApproach(String str) {
|
||||||
public void givenString_whenUsingSymmetricSubstringExpansion_thenFindLongestSymmetricSubstring() {
|
int maxLength = 1;
|
||||||
int start = 0;
|
|
||||||
int mid = 0;
|
|
||||||
int last_gt = 0;
|
|
||||||
int end = 0;
|
|
||||||
int best = 0;
|
|
||||||
|
|
||||||
while (start < input.length()) {
|
for (int i = 0; i < str.length(); i++) {
|
||||||
int current = Math.min(mid - start, end - mid);
|
for (int j = i; j < str.length(); j++) {
|
||||||
if (best < current) {
|
int flag = 1;
|
||||||
best = current;
|
for (int k = 0; k < (j - i + 1) / 2; k++) {
|
||||||
}
|
if (str.charAt(i + k) != str.charAt(j - k)) {
|
||||||
|
flag = 0;
|
||||||
if (end - mid == current && end < input.length()) {
|
break;
|
||||||
if (input.charAt(end) == '?') {
|
}
|
||||||
end++;
|
}
|
||||||
} else if (input.charAt(end) == '>') {
|
if (flag != 0 && (j - i + 1) > maxLength) {
|
||||||
end++;
|
maxLength = j - i + 1;
|
||||||
last_gt = end;
|
|
||||||
} else {
|
|
||||||
end++;
|
|
||||||
mid = end;
|
|
||||||
start = Math.max(start, last_gt);
|
|
||||||
}
|
}
|
||||||
} else if (mid < input.length() && input.charAt(mid) == '?') {
|
|
||||||
mid++;
|
|
||||||
} else if (start < mid) {
|
|
||||||
start++;
|
|
||||||
} else {
|
|
||||||
start = Math.max(start, last_gt);
|
|
||||||
mid++;
|
|
||||||
end = Math.max(mid, end);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int result = 2 * best;
|
return maxLength;
|
||||||
|
|
||||||
assertEquals(expected, result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenString_whenUsingBruteForce_thenFindLongestSymmetricSubstring() {
|
public void givenString_whenUsingBruteForce_thenFindLongestSymmetricSubstring() {
|
||||||
int max = 0;
|
assertEquals(expected, findLongestSymmetricSubstringUsingBruteForce(input).length());
|
||||||
for (int i = 0; i < input.length(); i++) {
|
}
|
||||||
for (int j = i + 1; j <= input.length(); j++) {
|
|
||||||
String t = input.substring(i, j);
|
@Test
|
||||||
if (t.length() % 2 == 0) {
|
public void givenString_whenUsingSymmetricSubstring_thenFindLongestSymmetricSubstring() {
|
||||||
int k = 0, l = t.length() - 1;
|
assertEquals(expected, findLongestSymmetricSubstringUsingSymmetricApproach(input));
|
||||||
boolean isSym = true;
|
}
|
||||||
while (k < l && isSym) {
|
|
||||||
if (!(t.charAt(k) == '<' || t.charAt(k) == '?') && (t.charAt(l) == '>' || t.charAt(l) == '?')) {
|
private String findLongestSymmetricSubstringUsingBruteForce(String str) {
|
||||||
isSym = false;
|
if (str == null || str.length() == 0) {
|
||||||
}
|
return "";
|
||||||
k++;
|
}
|
||||||
l--;
|
|
||||||
}
|
int maxLength = 0;
|
||||||
if (isSym) {
|
String longestPalindrome = "";
|
||||||
max = Math.max(max, t.length());
|
|
||||||
}
|
for (int i = 0; i < str.length(); i++) {
|
||||||
|
for (int j = i + 1; j <= str.length(); j++) {
|
||||||
|
String substring = str.substring(i, j);
|
||||||
|
if (isPalindrome(substring) && substring.length() > maxLength) {
|
||||||
|
maxLength = substring.length();
|
||||||
|
longestPalindrome = substring;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
assertEquals(expected, max);
|
return longestPalindrome;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isPalindrome(String s) {
|
||||||
|
int left = 0;
|
||||||
|
int right = s.length() - 1;
|
||||||
|
while (left < right) {
|
||||||
|
if (s.charAt(left) != s.charAt(right)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
left++;
|
||||||
|
right--;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user