Check if all array elements are present in a given stack or not | Function to check if all array elements is present in the stack ; Store the frequency of array elements ; Loop while the elements in the stack is not empty ; Condition to check if the element is present in the stack ; Driver Code
def checkArrInStack ( s , arr ) : NEW_LINE INDENT freq = { } NEW_LINE for ele in arr : NEW_LINE INDENT freq [ ele ] = freq . get ( ele , 0 ) + 1 NEW_LINE DEDENT while s : NEW_LINE INDENT poppedEle = s . pop ( ) NEW_LINE if poppedEle in freq : NEW_LINE freq [ poppedEle ] -= 1 NEW_LINE if not freq [ poppedEle ] : NEW_LINE INDENT del freq [ poppedEle ] NEW_LINE DEDENT DEDENT if not freq : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = [ 10 , 20 , 30 , 40 , 50 ] NEW_LINE arr = [ 20 , 30 ] NEW_LINE if checkArrInStack ( s , arr ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT