본문 바로가기

Algorithms/30 Days of Code

Day 18: Queues and Stacks

Welcome to Day 18! Today we're learning about Stacks and Queues. Check out the Tutorial tab for learning materials and an instructional video!

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, , is a palindrome?

To solve this challenge, we must first take each character in 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  isn't a palindrome).

Write the following declarations and implementations:

  1. Two instance variables: one for your , and one for your .
  2. void pushCharacter(char ch) method that pushes a character onto a stack.
  3. void enqueueCharacter(char ch) method that enqueues a character in the  instance variable.
  4. char popCharacter() method that pops and returns the character at the top of the  instance variable.
  5. char dequeueCharacter() method that dequeues and returns the first character in the  instance variable.

Input Format

You do not need to read anything from stdin. The locked stub code in your editor reads a single line containing string . It then calls the methods specified above to pass each character to your instance variables.

Constraints

  •  is composed of lowercase English letters.

Output Format

You are not responsible for printing any output to stdout. 
If your code is correctly written and  is a palindrome, the locked stub code will print ; otherwise, it will print 

Sample Input

racecar

Sample Output

The word, racecar, is a palindrome.


풀이


알고리즘 문제 관련해서 단골로 나오는 문제 중 하나인 palidrome.

대체로 int형 index 2개를 만들고index1은 0부터 index2는 word length의 -1부터 비교해서

맞으면 true 틀리면 false식의 문제였는데 stack과 queue로 손쉽게 해결

queue는 인터페이스라 likedlist로 객체구현


package Day18;


import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;

public class Solution {

private Queue<Character> queue;
private Stack<Character> stack;

public Solution(){
stack = new Stack<>();
queue = new LinkedList<>();
}

public void pushCharacter(char c){
stack.push(c);
}

public void enqueueCharacter(char c){
queue.add(c);
}

public Character popCharacter(){
return stack.pop();
}

public Character dequeueCharacter(){
return queue.remove();
}

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
scan.close();

// Convert input String to an array of characters:
char[] s = input.toCharArray();

// Create a Solution object:
Solution p = new Solution();

// Enqueue/Push all chars to their respective data structures:
for (char c : s) {
p.pushCharacter(c);
p.enqueueCharacter(c);
}

// Pop/Dequeue the chars at the head of both data structures and compare them:
boolean isPalindrome = true;
for (int i = 0; i < s.length/2; i++) {
if (p.popCharacter() != p.dequeueCharacter()) {
isPalindrome = false;
break;
}
}

//Finally, print whether string s is palindrome or not.
System.out.println( "The word, " + input + ", is "
+ ( (!isPalindrome) ? "not a palindrome." : "a palindrome." ) );
}
}


'Algorithms > 30 Days of Code' 카테고리의 다른 글

Day 20: Sorting  (0) 2018.03.08
Day 19: Interfaces  (0) 2018.02.27
Day 17: More Exceptions  (0) 2018.02.27
Day 16: Exceptions - String to Integer  (0) 2018.02.27
Day 15: Linked List  (0) 2018.02.23