Skip to content

Commit ae5290f

Browse files
authored
This commit is related to BAEL-7722 (#16382)
This commit aims to update SymmetricSubstringMaxLengthUnitTest.java.
1 parent 1c569bc commit ae5290f

1 file changed

Lines changed: 50 additions & 53 deletions

File tree

core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/symmetricsubstringlength/SymmetricSubstringMaxLengthUnitTest.java

Lines changed: 50 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,73 +5,70 @@
55
import static org.junit.Assert.assertEquals;
66

77
public class SymmetricSubstringMaxLengthUnitTest {
8-
String input = "<><??>>";
8+
String input = "abba";
99
int expected = 4;
1010

11-
@Test
12-
public void givenString_whenUsingSymmetricSubstringExpansion_thenFindLongestSymmetricSubstring() {
13-
int start = 0;
14-
int mid = 0;
15-
int last_gt = 0;
16-
int end = 0;
17-
int best = 0;
18-
19-
while (start < input.length()) {
20-
int current = Math.min(mid - start, end - mid);
21-
if (best < current) {
22-
best = current;
23-
}
11+
static int findLongestSymmetricSubstringUsingSymmetricApproach(String str) {
12+
int maxLength = 1;
2413

25-
if (end - mid == current && end < input.length()) {
26-
if (input.charAt(end) == '?') {
27-
end++;
28-
} else if (input.charAt(end) == '>') {
29-
end++;
30-
last_gt = end;
31-
} else {
32-
end++;
33-
mid = end;
34-
start = Math.max(start, last_gt);
14+
for (int i = 0; i < str.length(); i++) {
15+
for (int j = i; j < str.length(); j++) {
16+
int flag = 1;
17+
for (int k = 0; k < (j - i + 1) / 2; k++) {
18+
if (str.charAt(i + k) != str.charAt(j - k)) {
19+
flag = 0;
20+
break;
21+
}
22+
}
23+
if (flag != 0 && (j - i + 1) > maxLength) {
24+
maxLength = j - i + 1;
3525
}
36-
} else if (mid < input.length() && input.charAt(mid) == '?') {
37-
mid++;
38-
} else if (start < mid) {
39-
start++;
40-
} else {
41-
start = Math.max(start, last_gt);
42-
mid++;
43-
end = Math.max(mid, end);
4426
}
4527
}
46-
int result = 2 * best;
47-
48-
assertEquals(expected, result);
28+
return maxLength;
4929
}
5030

5131
@Test
5232
public void givenString_whenUsingBruteForce_thenFindLongestSymmetricSubstring() {
53-
int max = 0;
54-
for (int i = 0; i < input.length(); i++) {
55-
for (int j = i + 1; j <= input.length(); j++) {
56-
String t = input.substring(i, j);
57-
if (t.length() % 2 == 0) {
58-
int k = 0, l = t.length() - 1;
59-
boolean isSym = true;
60-
while (k < l && isSym) {
61-
if (!(t.charAt(k) == '<' || t.charAt(k) == '?') && (t.charAt(l) == '>' || t.charAt(l) == '?')) {
62-
isSym = false;
63-
}
64-
k++;
65-
l--;
66-
}
67-
if (isSym) {
68-
max = Math.max(max, t.length());
69-
}
33+
assertEquals(expected, findLongestSymmetricSubstringUsingBruteForce(input).length());
34+
}
35+
36+
@Test
37+
public void givenString_whenUsingSymmetricSubstring_thenFindLongestSymmetricSubstring() {
38+
assertEquals(expected, findLongestSymmetricSubstringUsingSymmetricApproach(input));
39+
}
40+
41+
private String findLongestSymmetricSubstringUsingBruteForce(String str) {
42+
if (str == null || str.length() == 0) {
43+
return "";
44+
}
45+
46+
int maxLength = 0;
47+
String longestPalindrome = "";
48+
49+
for (int i = 0; i < str.length(); i++) {
50+
for (int j = i + 1; j <= str.length(); j++) {
51+
String substring = str.substring(i, j);
52+
if (isPalindrome(substring) && substring.length() > maxLength) {
53+
maxLength = substring.length();
54+
longestPalindrome = substring;
7055
}
7156
}
7257
}
7358

74-
assertEquals(expected, max);
59+
return longestPalindrome;
7560
}
7661

62+
private boolean isPalindrome(String s) {
63+
int left = 0;
64+
int right = s.length() - 1;
65+
while (left < right) {
66+
if (s.charAt(left) != s.charAt(right)) {
67+
return false;
68+
}
69+
left++;
70+
right--;
71+
}
72+
return true;
73+
}
7774
}

0 commit comments

Comments
 (0)