CameraIcon
CameraIcon
SearchIcon
MyQuestionIcon
MyQuestionIcon
1
You visited us 1 times! Enjoying our articles? Unlock Full Access!
Question

A program attempts to generate as many permutations as possible of the string, 'abcd' by pushing the characters a, b, c, d in the same order onto a stack, but it may pop off the loop character at anytime. Which one of the following strings CANNOT be generated using this program?

A
cabd
Right on! Give the BNAT exam to get a 100% scholarship for BYJUS courses
B
cbad
No worries! We‘ve got your back. Try BYJU‘S free classes today!
C
abcd
No worries! We‘ve got your back. Try BYJU‘S free classes today!
D
dcba
No worries! We‘ve got your back. Try BYJU‘S free classes today!
Open in App
Solution

The correct option is A cabd
  • For every element push an element and pop immediately i.e. push (a) then pop will give permutation abcd.
  • For all elements push (a), push (b), push(c) and push(d) then pop, pop, pop, pop will give us dcba.
  • Push (a), push (b), push (c) then pop, pop, pop will give is cba, then push (d) and pop will gives us dcba.
  • Option (d) in not possible because a cannot pop after c and before 'b'.

flag
Suggest Corrections
thumbs-up
1
Join BYJU'S Learning Program
similar_icon
Related Videos
thumbnail
lock
What Is Multiplication?
MATHEMATICS
Watch in App
Join BYJU'S Learning Program
CrossIcon