-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPalindromeUsingStack.java
More file actions
47 lines (38 loc) · 1.7 KB
/
Copy pathPalindromeUsingStack.java
File metadata and controls
47 lines (38 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// PalindromeUsingStack.java
/**
* Problem: Check if a string is a palindrome using a stack.
*
* A palindrome is a string that reads the same forward and backward. We are asked to check if a given string
* is a palindrome by using a stack. The stack will be used to reverse the string and then compare each character.
*
* Approach:
* 1. Push all characters of the string onto a stack.
* 2. Pop characters from the stack and compare them with the original string.
* 3. If any character doesn't match, return false. If all characters match, return true.
*/
public class PalindromeUsingStack {
// Check if the string is palindrome using stack
public boolean isPalindrome(String s) {
LinkedListStack<Character> stack = new LinkedListStack<>();
// Step 1: Push all characters of the string onto the stack
for (char c : s.toCharArray()) {
stack.push(c); // Push the character onto the stack
}
// Step 2: Compare characters from the string with the stack's popped elements
for (int i = 0; i < s.length() / 2; i++) {
if (s.charAt(i) != stack.pop()) { // If characters do not match, return false
return false;
}
}
// Step 3: If all characters matched, return true
return true;
}
// Main function to test the solution
public static void main(String[] args) {
PalindromeUsingStack obj = new PalindromeUsingStack();
// Test with a palindrome string
System.out.println(obj.isPalindrome("ABCBA")); // Output: true
// Test with a non-palindrome string
System.out.println(obj.isPalindrome("ABCD")); // Output: false
}
}