Queues and Stacks problem solution 30 days of code HackerRank

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 the word, s is a palindrome number otherwise, it will print the word, s is not a palindrome number.
Queues and Stacks problem solution 30 days of code HackerRank

Problem solution in Java programming language.

import java.util.*;

public class Solution {
    Queue<Character> queue;
    Stack<Character> stack;

    Solution(){
        this.queue = new LinkedList<Character>();
        this.stack = new Stack<Character>();
    }

    void pushCharacter(char ch){
        this.stack.push(ch);
    }
    
    void enqueueCharacter(char ch){
        this.queue.add(ch);
    }
    
    char popCharacter(){
        return this.stack.pop();
    }

    char dequeueCharacter(){
        return this.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." ) );
    }
}

Second solution

public class Solution {
    Queue<Character> queue = new LinkedList<Character>();
    Stack<Character> stack = new Stack<Character>();

    void pushCharacter(char ch){
        this.stack.push(ch);
    }
    
    void enqueueCharacter(char ch){
        this.queue.add(ch);
    }
    
    char popCharacter(){
        return this.stack.pop();
    }

    char dequeueCharacter(){
        return this.queue.remove();
    }
    
    //...

Problem solution in the Swift programming language.

// See https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Generics.html
struct Stack<Element> {
    var items = [Element]()
    mutating func push(_ item: Element) {
        items.append(item);
    }
    mutating func pop() -> Element {
        return items.removeLast();
    }
}

struct Queue<Element> {
    var items = [Element]()
    mutating func enqueue(_ item: Element) {
        items.append(item);
    }
    mutating func dequeue() -> Element {
        return items.removeFirst();
    }
}


class Solution {

    var stack = Stack<Character>();
    var queue = Queue<Character>();

    func pushCharacter(ch: Character){
        stack.push(ch);
    }
    
    func popCharacter() -> Character {
        return stack.pop();
    }
    
    func enqueueCharacter(ch: Character){
        queue.enqueue(ch);
    }
    
    func dequeueCharacter() -> Character {
        return queue.dequeue();
    }
}

Problem solution in Python programming language.

import sys

class Solution:

    def __init__(self):
        self.stack = []
        self.queue = []
    
    def pushCharacter(self, ch):
        self.stack.append(ch)
    
    def enqueueCharacter(self, ch):
        self.queue.append(ch)
    
    def popCharacter(self):
        return self.stack.pop()
    
    def dequeueCharacter(self):
        return self.queue.pop(0)



# read the string s
s=input()
#Create the Solution class object
obj=Solution()   

l=len(s)
# push/enqueue all the characters of string s to stack
for i in range(l):
    obj.pushCharacter(s[i])
    obj.enqueueCharacter(s[i])
    
isPalindrome=True
'''
pop the top character from stack
dequeue the first character from queue
compare both the characters
''' 
for i in range(l // 2):
    if obj.popCharacter()!=obj.dequeueCharacter():
        isPalindrome=False
        break
#finally print whether string s is palindrome or not.
if isPalindrome:
    print("The word, "+s+", is a palindrome.")
else:
    print("The word, "+s+", is not a palindrome.")    

Post a Comment

0 Comments