Day 18: Queues and Stacks | 30 Days of Coding | HackerRank | Java Language

**Before watching this make sure that you’ve tried enough**
Language: Java 7

import java.io.*;
import java.util.*;

public class Solution {
    // Write your code here.
    
    int ms = 100;
    int top = -1,start = -1,end = -1;
    char stack[] = new char[100];
    char queue[] = new char[100];
    
    public void pushCharacter(char c){
        top++;
        stack[top] = c;
    }
    
    public char popCharacter(){
        top--;
        return stack[top+1];
    }
    
    public void enqueueCharacter(char c){
        end++;
        queue[end] = c;
    }
    
    public char dequeueCharacter(){
        start++;
        return queue[start];
    }
    
    
    
    
    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." ) );
    }
}

 

If this solution helped you, please share this with your friends.
And if you have anything to say, let me know in the comment section.
More solutions are coming so Like our Facebook page and Subscribe to our YouTube channel to stay connected.

Leave a Comment