-
Notifications
You must be signed in to change notification settings - Fork 183
/
19 - Day 18 - Queues and Stacks.cs
69 lines (56 loc) · 1.48 KB
/
19 - Day 18 - Queues and Stacks.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// ========================
// Information
// ========================
// Direct Link: https://www.hackerrank.com/challenges/30-queues-stacks/problem
// Difficulty: Easy
// Max Score: 30
// Language: C#
// ========================
// Solution
// ========================
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
public Stack < char > charS = new Stack < char > ();
public Queue < char > charQ = new Queue < char > ();
void pushCharacter(char c) {
charS.Push(c);
}
char popCharacter() {
return charS.Pop();
}
void enqueueCharacter(char c) {
charQ.Enqueue(c);
}
char dequeueCharacter() {
return charQ.Dequeue();
}
static void Main(String[] args) {
// read the string s.
string s = Console.ReadLine();
// create the Solution class object p.
Solution obj = new Solution();
// push/enqueue all the characters of string s to stack.
foreach(char c in s) {
obj.pushCharacter(c);
obj.enqueueCharacter(c);
}
bool isPalindrome = true;
// pop the top character from stack.
// dequeue the first character from queue.
// compare both the characters.
for (int i = 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);
}
}
}