Skip to content

Commit bd267bb

Browse files
TaranjeetSinghKalsiDebasish Biswas
and
Debasish Biswas
authored
Fixed error in Palindrome.java and gave semantic names to functions (TheAlgorithms#3643)
* fixed error and changed functions names fixed error at line 57 and gave semantic names to functions with comments * renamed functions renamed functions to match with original functions' names in the file * Updated TestCases Updated TestCases and changed a function name * Removed main() and changed function name Removed main() and changed the function name from isPalindromeStringBuilder to isPalindrome * fixed typo Co-authored-by: Debasish Biswas <debasishbsws.abc@gmail.com>
1 parent acf7a86 commit bd267bb

File tree

2 files changed

+19
-31
lines changed

2 files changed

+19
-31
lines changed

src/main/java/com/thealgorithms/strings/Palindrome.java

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,7 @@
66
class Palindrome {
77

88
/**
9-
* Driver Code
10-
*/
11-
public static void main(String[] args) {
12-
String[] palindromes = { null, "", "aba", "123321" };
13-
for (String s : palindromes) {
14-
assert isPalindrome(s) &&
15-
isPalindromeRecursion(s) &&
16-
isPalindrome1(s);
17-
}
18-
19-
String[] notPalindromes = { "abb", "abc", "abc123" };
20-
for (String s : notPalindromes) {
21-
assert !isPalindrome(s) &&
22-
!isPalindromeRecursion(s) &&
23-
!isPalindrome1(s);
24-
}
25-
}
26-
27-
/**
28-
* Check if a string is palindrome string or not
9+
* Check if a string is palindrome string or not using String Builder
2910
*
3011
* @param s a string to check
3112
* @return {@code true} if given string is palindrome, otherwise
@@ -54,17 +35,17 @@ public static boolean isPalindromeRecursion(String s) {
5435
return false;
5536
}
5637

57-
return isPalindrome(s.substring(1, s.length() - 1));
38+
return isPalindromeRecursion(s.substring(1, s.length() - 1));
5839
}
5940

6041
/**
61-
* Check if a string is palindrome string or not another way
42+
* Check if a string is palindrome string or not using two pointer technique
6243
*
6344
* @param s a string to check
6445
* @return {@code true} if given string is palindrome, otherwise
6546
* {@code false}
6647
*/
67-
public static boolean isPalindrome1(String s) {
48+
public static boolean isPalindromeTwoPointer(String s) {
6849
if (s == null || s.length() <= 1) {
6950
return true;
7051
}

src/test/java/com/thealgorithms/strings/PalindromeTest.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,20 @@ public class PalindromeTest {
77

88
@Test
99
public void palindrome() {
10-
String input1 = "kayak";
11-
String input2 = "kayaks";
12-
Assertions.assertTrue(Palindrome.isPalindrome(input1));
13-
Assertions.assertFalse(Palindrome.isPalindrome(input2));
14-
Assertions.assertTrue(Palindrome.isPalindromeRecursion(input1));
15-
Assertions.assertFalse(Palindrome.isPalindromeRecursion(input2));
16-
Assertions.assertTrue(Palindrome.isPalindrome1(input1));
17-
Assertions.assertFalse(Palindrome.isPalindrome1(input2));
10+
11+
String[] palindromes = { null, "", "aba", "123321", "kayak" };
12+
for (String s : palindromes) {
13+
Assertions.assertTrue(Palindrome.isPalindrome(s) &&
14+
Palindrome.isPalindromeRecursion(s) &&
15+
Palindrome.isPalindromeTwoPointer(s));
16+
}
17+
18+
String[] notPalindromes = { "abb", "abc", "abc123", "kayaks" };
19+
for (String s : notPalindromes) {
20+
Assertions.assertFalse(Palindrome.isPalindrome(s) ||
21+
Palindrome.isPalindromeRecursion(s) ||
22+
Palindrome.isPalindromeTwoPointer(s));
23+
}
24+
1825
}
1926
}

0 commit comments

Comments
 (0)