# HW6 ans - 1 Say you want to have a string of n as followed...

This preview shows pages 1–2. Sign up to view the full content.

1. Say you want to have a string of n a’s followed by n b’s. Pretend like your computer can only read character-by-character and cannot traverse backwards in the string. Show how you can use a stack to make sure you have n b’s after n a’s. As you read the string, push any a you encounter onto the stack. Once you start reading b’s, pop a’s for every b you encounter. If the stack is emptied by reading the last b, you pass. If the stack has something when the string has been read or if the stack becomes empty before reading the last b, then the string is not of the correct form. aaabbb - > pass aaabb - > fail aabbb - > fail 2. Similar problem to one, only we have n a’s followed by n b’s followed by n c’s. Again, assume the computer can only read character-by-character and can’t traverse backwards. Can you use one stack to make sure the string is of this type? If not, how many do you need? Run the same algorithm but with two stacks. When reading a’s push

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 3

HW6 ans - 1 Say you want to have a string of n as followed...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online