Skip to content

Commit 6006025

Browse files
authored
Add palindrome checker using stack (#5887)
1 parent 0f8cda9 commit 6006025

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.thealgorithms.stacks;
2+
3+
import java.util.LinkedList;
4+
5+
/**
6+
* A class that implements a palindrome checker using a stack.
7+
* The stack is used to store the characters of the string,
8+
* which we will pop one-by-one to create the string in reverse.
9+
*
10+
* Reference: https://www.geeksforgeeks.org/check-whether-the-given-string-is-palindrome-using-stack/
11+
*/
12+
public class PalindromeWithStack {
13+
private LinkedList<Character> stack;
14+
15+
/**
16+
* Constructs an empty stack that stores characters.
17+
*/
18+
public PalindromeWithStack() {
19+
stack = new LinkedList<Character>();
20+
}
21+
22+
/**
23+
* Check if the string is a palindrome or not.
24+
* Convert all characters to lowercase and push them into a stack.
25+
* At the same time, build a string
26+
* Next, pop from the stack and build the reverse string
27+
* Finally, compare these two strings
28+
*
29+
* @param string The string to check if it is palindrome or not.
30+
*/
31+
public boolean checkPalindrome(String string) {
32+
// Create a StringBuilder to build the string from left to right
33+
StringBuilder stringBuilder = new StringBuilder(string.length());
34+
// Convert all characters to lowercase
35+
String lowercase = string.toLowerCase();
36+
37+
// Iterate through the string
38+
for (int i = 0; i < lowercase.length(); ++i) {
39+
char c = lowercase.charAt(i);
40+
// Build the string from L->R
41+
stringBuilder.append(c);
42+
// Push to the stack
43+
stack.push(c);
44+
}
45+
46+
// The stack contains the reverse order of the string
47+
StringBuilder reverseString = new StringBuilder(stack.size());
48+
// Until the stack is not empty
49+
while (!stack.isEmpty()) {
50+
// Build the string from R->L
51+
reverseString.append(stack.pop());
52+
}
53+
54+
// Finally, compare the L->R string with the R->L string
55+
return reverseString.toString().equals(stringBuilder.toString());
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.thealgorithms.stacks;
2+
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
9+
public class PalindromeWithStackTest {
10+
11+
private PalindromeWithStack palindromeChecker;
12+
13+
@BeforeEach
14+
public void setUp() {
15+
palindromeChecker = new PalindromeWithStack();
16+
}
17+
18+
@Test
19+
public void testValidOne() {
20+
String testString = "Racecar";
21+
assertTrue(palindromeChecker.checkPalindrome(testString));
22+
}
23+
24+
@Test
25+
public void testInvalidOne() {
26+
String testString = "James";
27+
assertFalse(palindromeChecker.checkPalindrome(testString));
28+
}
29+
30+
@Test
31+
public void testValidTwo() {
32+
String testString = "madam";
33+
assertTrue(palindromeChecker.checkPalindrome(testString));
34+
}
35+
36+
@Test
37+
public void testInvalidTwo() {
38+
String testString = "pantry";
39+
assertFalse(palindromeChecker.checkPalindrome(testString));
40+
}
41+
42+
@Test
43+
public void testValidThree() {
44+
String testString = "RaDar";
45+
assertTrue(palindromeChecker.checkPalindrome(testString));
46+
}
47+
48+
@Test
49+
public void testInvalidThree() {
50+
String testString = "Win";
51+
assertFalse(palindromeChecker.checkPalindrome(testString));
52+
}
53+
54+
@Test
55+
public void testBlankString() {
56+
String testString = "";
57+
assertTrue(palindromeChecker.checkPalindrome(testString));
58+
}
59+
60+
@Test
61+
public void testStringWithNumbers() {
62+
String testString = "12321";
63+
assertTrue(palindromeChecker.checkPalindrome(testString));
64+
}
65+
66+
@Test
67+
public void testStringWithNumbersTwo() {
68+
String testString = "12325";
69+
assertFalse(palindromeChecker.checkPalindrome(testString));
70+
}
71+
72+
@Test
73+
public void testStringWithNumbersAndLetters() {
74+
String testString = "po454op";
75+
assertTrue(palindromeChecker.checkPalindrome(testString));
76+
}
77+
}

0 commit comments

Comments
 (0)