-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay18-Queues-and-Stacks
45 lines (32 loc) · 1.73 KB
/
Day18-Queues-and-Stacks
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
Problem:
Welcome to Day 18! Today we're learning about Stacks and Queues. Check out the Tutorial tab for learning materials and an
instructional video!
A palindrome is a word, phrase, number, or other sequence of characters which reads the same backwards and forwards.
Can you determine if a given string, s, is a palindrome?
To solve this challenge, we must first take each character in s ,enqueue it in a queue, and also push that same character onto a stack.
Once that's done, we must dequeue the first character from the queue and pop the top character off the stack,
then compare the two characters to see if they are the same; as long as the characters match, we continue dequeueing,
popping, and comparing each character until our containers are empty (a non-match means s isn't a palindrome).
Write the following declarations and implementations:
1. Two instance variables: one for your stack, and one for your queue.
2. A void pushCharacter(char ch) method that pushes a character onto a stack.
3. A void enqueueCharacter(char ch) method that enqueues a character in the queue instance variable.
4. A char popCharacter() method that pops and returns the character at the top of the stack instance variable.
5. A char dequeueCharacter() method that dequeues and returns the first character in the queue instance variable.
Solution:
public class Solution {
// Write your code here.
Stack<Character> stack = new Stack<Character>();
Queue<Character> coda = new LinkedList<Character>();
void pushCharacter(char ch) {
stack.push(ch);
}
void enqueueCharacter(char ch) {
coda.add(ch);
}
char popCharacter() {
return stack.pop();
}
char dequeueCharacter() {
return coda.remove();
}