Wonder String
Question Breif
Given a string
containing just the characters 'a', 'b', 'c', 'd', 'e' and 'f',
determine if the input string is wonder or not.
Pairs are known as
(opening element,closing element) : (a,b), (c,d), (e,f)
An input string is
wonder if:
- First(opening element) element of pair must be closed by the second(closing element) element of the pair
-
Opening element must be closed by respective closing element in the correct order
-
Note that an empty string is also considered wonder string:
Link To Question:
https://www.hackerrank.com/contests/mindstormer-apr20/challenges/wonder-string-1
Approach:
Hint: Use Stack
If an opening element occurs we push it into stack and when the closing element occurs, we check if the element at the top of stack is corresponding opening element . If its some other element then we return false. Else we pop out the top most element. If at the end of string stack is empty then we return true.
Another way to think : We can have (), {}, [] instead of ab , cd , ef.
Here is the Solution:
Comments
Post a Comment