We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
classSolution{//Write your code hereQueue<char>queue=newQueue<char>();Stack<char>stack=newStack<char>();publicvoidpushCharacter(charc){stack.Push(c);}publiccharpopCharacter(){returnstack.Pop();}publicvoidenqueueCharacter(charc){queue.Enqueue(c);}publicchardequeueCharacter(){returnqueue.Dequeue();}staticvoidMain(String[]args){// read the string s.strings=Console.ReadLine();// create the Solution class object p.Solutionobj=newSolution();// push/enqueue all the characters of string s to stack.foreach(charcins){obj.pushCharacter(c);obj.enqueueCharacter(c);}boolisPalindrome=true;// pop the top character from stack.// dequeue the first character from queue.// compare both the characters.for(inti=0;i<s.Length/2;i++){if(obj.popCharacter()!=obj.dequeueCharacter()){isPalindrome=false;break;}}// finally print whether string s is palindrome or not.if(isPalindrome){Console.Write("The word, {0}, is a palindrome.",s);}else{Console.Write("The word, {0}, is not a palindrome.",s);}}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 18: Queues and Stacks
You are viewing a single comment's thread. Return to all comments →
C# code: