123,992 views
31 votes
31 votes
palindrome is a string that reads the same forwards as backwards. Using only a xed number of stacks, and a xed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as appropriate

User Anmol Agrawal
by
2.9k points

1 Answer

7 votes
7 votes

Solution :

check_palindrome
$(string)$

lower_
$case$_string
$=$ string
$. to$_lower()

Let stack = new Stack()

Let queue = new Queue();

for each character c in lower_case_string:

stack.push(c);

queue.enqueue(c);

let isPalindrome = true;

while queue is not empty {

if (queue.remove().equals(stack.pop())) {

continue;

} else {

isPalindrome=false;

break while loop;

}

}

return isPalindrome

Input = aabb

output = true

input =abcd

output = false

User Armin Primadi
by
2.9k points