Repository: TheAlgorithms/Java
Pull request #5887 (2024-10-18 08:59:01)

Added palindrome checker using stack
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [X] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [X] This pull request is all my own work -- I have not plagiarized it.
- [X] All filenames are in PascalCase.
- [X] All functions and variable names follow Java naming conventions.
- [X] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [X] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`


Patch:
diff --git a/src/main/java/com/thealgorithms/stacks/PalindromeWithStack.java b/src/main/java/com/thealgorithms/stacks/PalindromeWithStack.java
new file mode 100644
index 00000000..98c43934
--- /dev/null
+++ b/src/main/java/com/thealgorithms/stacks/PalindromeWithStack.java
@@ -0,0 +1,57 @@
+package com.thealgorithms.stacks;
+
+import java.util.LinkedList;
+
+/**
+ * A class that implements a palindrome checker using a stack.
+ * The stack is used to store the characters of the string,
+ * which we will pop one-by-one to create the string in reverse.
+ *
+ * Reference: https://www.geeksforgeeks.org/check-whether-the-given-string-is-palindrome-using-stack/
+ */
+public class PalindromeWithStack {
+    private LinkedList<Character> stack;
+
+    /**
+     * Constructs an empty stack that stores characters.
+     */
+    public PalindromeWithStack() {
+        stack = new LinkedList<Character>();
+    }
+
+    /**
+     * Check if the string is a palindrome or not.
+     * Convert all characters to lowercase and push them into a stack.
+     * At the same time, build a string
+     * Next, pop from the stack and build the reverse string
+     * Finally, compare these two strings
+     *
+     * @param string The string to check if it is palindrome or not.
+     */
+    public boolean checkPalindrome(String string) {
+        // Create a StringBuilder to build the string from left to right
+        StringBuilder stringBuilder = new StringBuilder(string.length());
+        // Convert all characters to lowercase
+        String lowercase = string.toLowerCase();
+
+        // Iterate through the string
+        for (int i = 0; i < lowercase.length(); ++i) {
+            char c = lowercase.charAt(i);
+            // Build the string from L->R
+            stringBuilder.append(c);
+            // Push to the stack
+            stack.push(c);
+        }
+
+        // The stack contains the reverse order of the string
+        StringBuilder reverseString = new StringBuilder(stack.size());
+        // Until the stack is not empty
+        while (!stack.isEmpty()) {
+            // Build the string from R->L
+            reverseString.append(stack.pop());
+        }
+
+        // Finally, compare the L->R string with the R->L string
+        return reverseString.toString().equals(stringBuilder.toString());
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/stacks/PalindromeWithStackTest.java b/src/test/java/com/thealgorithms/stacks/PalindromeWithStackTest.java
new file mode 100644
index 00000000..47b21d5e
--- /dev/null
+++ b/src/test/java/com/thealgorithms/stacks/PalindromeWithStackTest.java
@@ -0,0 +1,77 @@
+package com.thealgorithms.stacks;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+public class PalindromeWithStackTest {
+
+    private PalindromeWithStack palindromeChecker;
+
+    @BeforeEach
+    public void setUp() {
+        palindromeChecker = new PalindromeWithStack();
+    }
+
+    @Test
+    public void testValidOne() {
+        String testString = "Racecar";
+        assertTrue(palindromeChecker.checkPalindrome(testString));
+    }
+
+    @Test
+    public void testInvalidOne() {
+        String testString = "James";
+        assertFalse(palindromeChecker.checkPalindrome(testString));
+    }
+
+    @Test
+    public void testValidTwo() {
+        String testString = "madam";
+        assertTrue(palindromeChecker.checkPalindrome(testString));
+    }
+
+    @Test
+    public void testInvalidTwo() {
+        String testString = "pantry";
+        assertFalse(palindromeChecker.checkPalindrome(testString));
+    }
+
+    @Test
+    public void testValidThree() {
+        String testString = "RaDar";
+        assertTrue(palindromeChecker.checkPalindrome(testString));
+    }
+
+    @Test
+    public void testInvalidThree() {
+        String testString = "Win";
+        assertFalse(palindromeChecker.checkPalindrome(testString));
+    }
+
+    @Test
+    public void testBlankString() {
+        String testString = "";
+        assertTrue(palindromeChecker.checkPalindrome(testString));
+    }
+
+    @Test
+    public void testStringWithNumbers() {
+        String testString = "12321";
+        assertTrue(palindromeChecker.checkPalindrome(testString));
+    }
+
+    @Test
+    public void testStringWithNumbersTwo() {
+        String testString = "12325";
+        assertFalse(palindromeChecker.checkPalindrome(testString));
+    }
+
+    @Test
+    public void testStringWithNumbersAndLetters() {
+        String testString = "po454op";
+        assertTrue(palindromeChecker.checkPalindrome(testString));
+    }
+}