diff --git a/src/main/java/com/thealgorithms/strings/Palindrome.java b/src/main/java/com/thealgorithms/strings/Palindrome.java deleted file mode 100644 index 3567a371d70e..000000000000 --- a/src/main/java/com/thealgorithms/strings/Palindrome.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.thealgorithms.strings; - -/** - * Wikipedia: https://en.wikipedia.org/wiki/Palindrome - */ -final class Palindrome { - private Palindrome() { - } - - /** - * 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 - * {@code false} - */ - public static boolean isPalindrome(String s) { - return ((s == null || s.length() <= 1) || s.equals(new StringBuilder(s).reverse().toString())); - } - - /** - * Check if a string is palindrome string or not using recursion - * - * @param s a string to check - * @return {@code true} if given string is palindrome, otherwise - * {@code false} - */ - public static boolean isPalindromeRecursion(String s) { - if (s == null || s.length() <= 1) { - return true; - } - - if (s.charAt(0) != s.charAt(s.length() - 1)) { - return false; - } - - return isPalindromeRecursion(s.substring(1, s.length() - 1)); - } - - /** - * 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 isPalindromeTwoPointer(String s) { - if (s == null || s.length() <= 1) { - return true; - } - for (int i = 0, j = s.length() - 1; i < j; ++i, --j) { - if (s.charAt(i) != s.charAt(j)) { - return false; - } - } - return true; - } -} diff --git a/src/main/java/com/thealgorithms/strings/TwoPointerPalindrome.java b/src/main/java/com/thealgorithms/strings/TwoPointerPalindrome.java new file mode 100644 index 000000000000..4f029e527cc2 --- /dev/null +++ b/src/main/java/com/thealgorithms/strings/TwoPointerPalindrome.java @@ -0,0 +1,50 @@ +/** + * Check if a given string is a palindrome using the two-pointer technique. + * A palindrome is a string that reads the same forward and backward. + * + * Example: "level", "madam", "12321" are palindromes. + * + * Author: Sushma + */ + +package com.thealgorithms.strings; + +public class TwoPointerPalindrome { + + /** + * This method checks if the given string is a palindrome using two pointers. + * + * @param s The input string to check + * @return true if the string is a palindrome, false otherwise + */ + public static boolean isPalindrome(String s) { + + // If the string is null or has only 1 or 0 characters, it's a palindrome + if (s == null || s.length() <= 1) { + return true; + } + + // Initialize two pointers: left starting from the beginning, + // right starting from the end of the string + int left = 0; + int right = s.length() - 1; + + // Loop until the two pointers meet in the middle + while (left < right) { + + // If characters at left and right don't match, it's not a palindrome + if (s.charAt(left) != s.charAt(right)) { + return false; + } + + // Move the left pointer to the right + left++; + + // Move the right pointer to the left + right--; + } + + // If all characters matched, then it's a palindrome + return true; + } +} diff --git a/src/test/java/com/thealgorithms/strings/PalindromeTest.java b/src/test/java/com/thealgorithms/strings/PalindromeTest.java deleted file mode 100644 index d839e381c6dd..000000000000 --- a/src/test/java/com/thealgorithms/strings/PalindromeTest.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.thealgorithms.strings; - -import java.util.stream.Stream; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.MethodSource; - -public class PalindromeTest { - private static Stream provideTestCases() { - return Stream.of(new TestData(null, true), new TestData("", true), new TestData("aba", true), new TestData("123321", true), new TestData("kayak", true), new TestData("abb", false), new TestData("abc", false), new TestData("abc123", false), new TestData("kayaks", false)); - } - - @ParameterizedTest - @MethodSource("provideTestCases") - void testPalindrome(TestData testData) { - Assertions.assertEquals(testData.expected, Palindrome.isPalindrome(testData.input) && Palindrome.isPalindromeRecursion(testData.input) && Palindrome.isPalindromeTwoPointer(testData.input)); - } - - private record TestData(String input, boolean expected) { - } -} diff --git a/src/test/java/com/thealgorithms/strings/TwoPointerPalindromeTest.java b/src/test/java/com/thealgorithms/strings/TwoPointerPalindromeTest.java new file mode 100644 index 000000000000..40f2f4e991a3 --- /dev/null +++ b/src/test/java/com/thealgorithms/strings/TwoPointerPalindromeTest.java @@ -0,0 +1,20 @@ +package com.thealgorithms.strings; + +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + +public class TwoPointerPalindromeTest { + + @Test + void testPalindrome() { + assertTrue(TwoPointerPalindrome.isPalindrome("madam")); + assertTrue(TwoPointerPalindrome.isPalindrome("racecar")); + assertTrue(TwoPointerPalindrome.isPalindrome("a")); + + assertTrue(TwoPointerPalindrome.isPalindrome(null)); +assertTrue(TwoPointerPalindrome.isPalindrome("")); + + assertFalse(TwoPointerPalindrome.isPalindrome("hello")); + assertFalse(TwoPointerPalindrome.isPalindrome("world")); + } +}