Skip to content

Fixed error in Palindrome.java and gave semantic names to functions #3643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 4 additions & 23 deletions src/main/java/com/thealgorithms/strings/Palindrome.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,7 @@
class Palindrome {

/**
* Driver Code
*/
public static void main(String[] args) {
String[] palindromes = { null, "", "aba", "123321" };
for (String s : palindromes) {
assert isPalindrome(s) &&
isPalindromeRecursion(s) &&
isPalindrome1(s);
}

String[] notPalindromes = { "abb", "abc", "abc123" };
for (String s : notPalindromes) {
assert !isPalindrome(s) &&
!isPalindromeRecursion(s) &&
!isPalindrome1(s);
}
}

/**
* Check if a string is palindrome string or not
* Check if a string is palindrome string or not using String Builder
*
* @param s a string to check
* @return {@code true} if given string is palindrome, otherwise
Expand Down Expand Up @@ -54,17 +35,17 @@ public static boolean isPalindromeRecursion(String s) {
return false;
}

return isPalindrome(s.substring(1, s.length() - 1));
return isPalindromeRecursion(s.substring(1, s.length() - 1));
}

/**
* Check if a string is palindrome string or not another way
* Check if a string is palindrome string or not using two pointer technique
*
* @param s a string to check
* @return {@code true} if given string is palindrome, otherwise
* {@code false}
*/
public static boolean isPalindrome1(String s) {
public static boolean isPalindromeTwoPointer(String s) {
if (s == null || s.length() <= 1) {
return true;
}
Expand Down
23 changes: 15 additions & 8 deletions src/test/java/com/thealgorithms/strings/PalindromeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@ public class PalindromeTest {

@Test
public void palindrome() {
String input1 = "kayak";
String input2 = "kayaks";
Assertions.assertTrue(Palindrome.isPalindrome(input1));
Assertions.assertFalse(Palindrome.isPalindrome(input2));
Assertions.assertTrue(Palindrome.isPalindromeRecursion(input1));
Assertions.assertFalse(Palindrome.isPalindromeRecursion(input2));
Assertions.assertTrue(Palindrome.isPalindrome1(input1));
Assertions.assertFalse(Palindrome.isPalindrome1(input2));

String[] palindromes = { null, "", "aba", "123321", "kayak" };
for (String s : palindromes) {
Assertions.assertTrue(Palindrome.isPalindrome(s) &&
Palindrome.isPalindromeRecursion(s) &&
Palindrome.isPalindromeTwoPointer(s));
}

String[] notPalindromes = { "abb", "abc", "abc123", "kayaks" };
for (String s : notPalindromes) {
Assertions.assertFalse(Palindrome.isPalindrome(s) ||
Palindrome.isPalindromeRecursion(s) ||
Palindrome.isPalindromeTwoPointer(s));
}

}
}