Update SymmetricSubstringMaxLengthUnitTest.java (#16411)

This commit is contained in:
Mo Helmy 2024-04-15 20:47:38 +02:00 committed by GitHub
parent 2ea4c541bd
commit f3548a3c44
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 4 additions and 6 deletions

View File

@ -30,7 +30,7 @@ public class SymmetricSubstringMaxLengthUnitTest {
@Test @Test
public void givenString_whenUsingBruteForce_thenFindLongestSymmetricSubstring() { public void givenString_whenUsingBruteForce_thenFindLongestSymmetricSubstring() {
assertEquals(expected, findLongestSymmetricSubstringUsingBruteForce(input).length()); assertEquals(expected, findLongestSymmetricSubstringUsingBruteForce(input));
} }
@Test @Test
@ -38,25 +38,23 @@ public class SymmetricSubstringMaxLengthUnitTest {
assertEquals(expected, findLongestSymmetricSubstringUsingSymmetricApproach(input)); assertEquals(expected, findLongestSymmetricSubstringUsingSymmetricApproach(input));
} }
private String findLongestSymmetricSubstringUsingBruteForce(String str) { private int findLongestSymmetricSubstringUsingBruteForce(String str) {
if (str == null || str.length() == 0) { if (str == null || str.length() == 0) {
return ""; return 0;
} }
int maxLength = 0; int maxLength = 0;
String longestPalindrome = "";
for (int i = 0; i < str.length(); i++) { for (int i = 0; i < str.length(); i++) {
for (int j = i + 1; j <= str.length(); j++) { for (int j = i + 1; j <= str.length(); j++) {
String substring = str.substring(i, j); String substring = str.substring(i, j);
if (isPalindrome(substring) && substring.length() > maxLength) { if (isPalindrome(substring) && substring.length() > maxLength) {
maxLength = substring.length(); maxLength = substring.length();
longestPalindrome = substring;
} }
} }
} }
return longestPalindrome; return maxLength;
} }
private boolean isPalindrome(String s) { private boolean isPalindrome(String s) {