TheBulgarianWolf

Queues and Stacks Hackerrank

May 24th, 2021
925
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.65 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Collections;
  5.  
  6. class Solution {
  7.     //Write your code here
  8.     Stack<char> myStack;
  9.     Queue<char> queue;
  10.  
  11.     public Solution()
  12.     {
  13.         myStack = new Stack<char>();
  14.         queue = new Queue<char>();
  15.     }
  16.    
  17.     void pushCharacter(char ch){
  18.         myStack.Push(ch);
  19.     }
  20.     void enqueueCharacter(char ch){
  21.         queue.Enqueue(ch);
  22.     }
  23.    
  24.     char popCharacter(){
  25.         return myStack.Pop();
  26.     }
  27.    
  28.     char dequeueCharacter(){
  29.         return queue.Dequeue();
  30.     }
  31.  
  32.     static void Main(String[] args) {
  33.         // read the string s.
  34.         string s = Console.ReadLine();
  35.        
  36.         // create the Solution class object p.
  37.         Solution obj = new Solution();
  38.        
  39.         // push/enqueue all the characters of string s to stack.
  40.         foreach (char c in s) {
  41.             obj.pushCharacter(c);
  42.             obj.enqueueCharacter(c);
  43.         }
  44.        
  45.         bool isPalindrome = true;
  46.        
  47.         // pop the top character from stack.
  48.         // dequeue the first character from queue.
  49.         // compare both the characters.
  50.         for (int i = 0; i < s.Length / 2; i++) {
  51.             if (obj.popCharacter() != obj.dequeueCharacter()) {
  52.                 isPalindrome = false;
  53.                
  54.                 break;
  55.             }
  56.         }
  57.        
  58.         // finally print whether string s is palindrome or not.
  59.         if (isPalindrome) {
  60.             Console.Write("The word, {0}, is a palindrome.", s);
  61.         } else {
  62.             Console.Write("The word, {0}, is not a palindrome.", s);
  63.         }
  64.     }
  65. }
Add Comment
Please, Sign In to add comment