Created
April 21, 2018 00:53
-
-
Save AlinaWithAFace/0c2aa85fddd4c7a746a6935214cc55f0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.*; | |
public class Solution { | |
// Write your code here. | |
Stack<String> stack = new Stack<>(); | |
Queue<String> queue = new LinkedList<>(); | |
void pushCharacter(char ch) { | |
stack.push(String.valueOf(ch)); | |
} | |
void enqueueCharacter(char ch) { | |
queue.add(String.valueOf(ch)); | |
} | |
char popCharacter() { | |
return stack.pop().toCharArray()[0]; | |
} | |
char dequeueCharacter() { | |
return queue.remove().toCharArray()[0]; | |
} | |
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.")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment