Skip to main content

Day 18: Queues and Stacks Hackerrank Solution Java

 Day 18: Queues and Stacks Hackerrank Solution Java

For Explanation Check Video:


Sample Input

racecar

Sample Output

The word, racecar, is a palindrome.
Code:
import java.io.*;
import java.util.*;

public class Solution {
    // Write your code here.//racecar
    Stack<Character> st = new Stack<>();
    Queue<Character> q = new LinkedList<>();
    void pushCharacter(char ch){
        st.push(ch);//race
    }
    void enqueueCharacter(char ch){
        q.add(ch);//ecar
    }
    char popCharacter(){
        return st.pop();//c
    }
    char dequeueCharacter(){
        return q.remove();//c
    }
    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." ) );
    }
}

Comments