We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Algorithms
- Strings
- Morgan and a String
- Discussions
Morgan and a String
Morgan and a String
Sort by
recency
|
308 Discussions
|
Please Login in order to post a comment
Use this C++ Code
so use this for good solution
If the tops aren't the same, you should pick the smallest.
The key to the problem is considering what should happen if the tops are the same. We are interested in the first character that is different from the current top. Take for example the stacks BBBA... and BBBB... in this case you will want to consume the left stack first, so that yo u can put the A earlier in the string. Therefore you can compare the entire string to arrive at the solution, as BBBA... < BBBB... returns True. However, there is a special case where the entire substring fits in the other. Consider CCC and CCC... Then CCC < CCC... will return True. However we will always want to consume the longer stack: Consider CCC, CCCA. -> Then CCCACCC is better than CCCCCCCA. Consider CCC, CCCD. -> Consuming CCC gives us option to start consuming CCC or D. End up optimal with CCCCCCD.
To fix this you can append any character c that has [A-Z] < c. Then CCCa < CCC...a will return False and instruct us to go for the longest stack
There is only case 1 solution. I do not understand why others can not pass test. Can you give me advice about that ?