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, , 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:
- Two instance variables: one for your , and one for your .
- A void pushCharacter(char ch) method that pushes a character onto a stack.
- A void enqueueCharacter(char ch) method that enqueues a character in the instance variable.
- A char popCharacter() method that pops and returns the character at the top of the instance variable.
- A 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.
How I solved the problem
# stack은 first in, last out,
# queue는 first in, first out 의 원칙이 있음을 잘 이해하기!
#Stack은 Stack class를 사용하고 Queue는 주로 LinkedList로 사용함..!
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 48 49 | import java.io.*; import java.util.*; public class Solution { // Write your code here. Stack<Character> stackStr = new Stack<Character>(); //stack LinkedList<Character> queueStr = new LinkedList<Character>(); //pushes a character onto a stack public void pushCharacter(char character) { stackStr.push(character); } //enqueues a caracter in the queue instance variable public void enqueueCharacter(char character) { queueStr.add(character); } //pop and return the character at the top of the stack instance variable public char popCharacter() { return stackStr.pop(); //stack에서 맨 상위의 값을 return한 후 제거함 } //dequeues and returns the first charater in the queue instance variable public char dequeueCharacter() { return queueStr.poll();//queueStr에서 맨 처음의 값을 return한 후 제거함 } 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." ) ); } } | cs |
[출처 : https://www.hackerrank.com ]
'1 Day 1 Algorithms' 카테고리의 다른 글
[2019.01.09] Sorting (0) | 2019.01.09 |
---|---|
[2019.01.08] Interfaces (0) | 2019.01.08 |
[2019.01.04] More Exceptions (0) | 2019.01.04 |
[2019.01.03] Exceptions - String to Integer (0) | 2019.01.03 |
[2019.01.02] Linked List (0) | 2019.01.02 |
댓글