|
5 | 5 | import static org.junit.Assert.assertEquals; |
6 | 6 |
|
7 | 7 | public class SymmetricSubstringMaxLengthUnitTest { |
8 | | - String input = "<><??>>"; |
| 8 | + String input = "abba"; |
9 | 9 | int expected = 4; |
10 | 10 |
|
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; |
24 | 13 |
|
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; |
35 | 25 | } |
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); |
44 | 26 | } |
45 | 27 | } |
46 | | - int result = 2 * best; |
47 | | - |
48 | | - assertEquals(expected, result); |
| 28 | + return maxLength; |
49 | 29 | } |
50 | 30 |
|
51 | 31 | @Test |
52 | 32 | 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; |
70 | 55 | } |
71 | 56 | } |
72 | 57 | } |
73 | 58 |
|
74 | | - assertEquals(expected, max); |
| 59 | + return longestPalindrome; |
75 | 60 | } |
76 | 61 |
|
| 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 | + } |
77 | 74 | } |
0 commit comments