Practice on Toph

Participate in exhilarating programming contests, solve unique algorithm and data structure challenges and be a part of an awesome community.

Balanced Tag

By Shibli_sesh · Limits 1s, 512 MB

Akash likes Balanced Parentheses. For example -

1. (Hello [Akash] how are you {?}) 
2. ((Hello [Akash] how are you {?}})

Akash likes expression 1 but he doesnot like expression 2. In this problem, you have to help Akash by telling if a given expression consisting of TAGS ( <> or </> or X ) is balanced or not. The right orders of the expressions consisting of tags are <X> or <X/> or </> or <> or X , where X can be a certain length of string consisting of lower case english letters (‘a’-‘z’), space (' ') and these characters ('<', '>'). Note that, any characters after / without > should be considered Invalid.

For example -

<>Hello <>Akash</> how are you? </>  [Balanced Tag]
</>Hello </>Akash<> how are you? <>   [Balanced Tag]
Hello Akash     [Balanced Tag]
<item>what are you looking at?<item/> (Balanced Tag)
<item/>Why are you looking at it?<abcd/>    (Balanced tag)
<></item>      (Not Balanced)


Input will consist of several lines ending in EOF. Each line will contain a non-empty string made up of lower case English letters (‘a’-‘z’), space (' ') and these characters ('<', '>', '/'). The length of the string will be less than or equal to 100. There will be at most 200000 lines per file.


For each line of input, output will consist of exactly one line that includes either "Balanced Tag" or "Not Balanced" without quotation mark. See the sample i/o for better understanding.


Balanced Tag
Balanced Tag
Not Balanced
Balanced Tag
Not Balanced
Balanced Tag
Balanced Tag



74% Solution Ratio

ktoufiqueeEarliest, Dec '20

Deshi_TouristFastest, 0.1s

ktoufiqueeLightest, 2.6 MB

FAHIM.ctgShortest, 773B


Login to submit

Toph uses cookies. By continuing you agree to our Cookie Policy.