Update SymmetricSubstringMaxLengthUnitTest.java (#16411)
This commit is contained in:
parent
2ea4c541bd
commit
f3548a3c44
|
@ -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) {
|
||||||
|
|
Loading…
Reference in New Issue