Day 18: Queues and Stacks

  • Instructions:
    • Determine if a given string s is a palindrome (word, phrase, number, or other sequence of characters which reads the same backwards and forwards)
    • First take each character in s, enqueue it in a queue, and also push that same character onto a stack.
    • Then dequeue the first character from the queue and pop the top character off the stack, then compare the two characters to see if they are the same; as long as the characters match, we continue dequeueing, popping, and comparing each character until our containers are empty (a non-match means isn’t a palindrome).
    • Constraint:
      • s is composed of lowercase English letters.
    Excercise
    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
    
    import sys
    
    class Solution:
        # Write your code here
    
    # 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.")   
    
    Solution
    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
    
    import sys
    
    class Solution:
        # Write your code here
        def __init__(self):
            self.stack = []
            self.queue = []
        
        def pushCharacter(self, char):
            self.stack.insert(0,char)
        
        def popCharacter(self):
            return self.stack.pop(0)
            
        def enqueueCharacter(self,char):
            self.queue.append(char)
            
        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.")