Skip to content

Commit b193a0a

Browse files
committed
added palindrome checker using stack
1 parent c301fec commit b193a0a

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 final 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 final boolean checkPalindrome(final 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+
if (c >= 'a' && c <= 'z') {
41+
// Build the string from L->R
42+
stringBuilder.append(c);
43+
// Push to the stack
44+
stack.push(c);
45+
}
46+
}
47+
48+
// The stack contains the reverse order of the string
49+
StringBuilder reverseString = new StringBuilder(stack.size());
50+
// Until the stack is not empty
51+
while (!stack.isEmpty()){
52+
// Build the string from R->L
53+
reverseString.append(stack.pop());
54+
}
55+
56+
// Finally, compare the L->R string with the R->L string
57+
return reverseString.toString().equals(stringBuilder.toString());
58+
}
59+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
}

0 commit comments

Comments
 (0)