Tag Content Extractor in Java : Hacker Rank Solution : Digit Wood

Java
,

In a tag-based language like XML or HTML, contents are enclosed between a start tag and an end tag like <tag>contents</tag>. Note that the corresponding end tag starts with a /.

Given a string of text in a tag-based language, parse this text and retrieve the contents enclosed within sequences of well-organized tags meeting the following criterion:

  1. The name of the start and end tags must be same. The HTML code <h1>Hello World</h2> is not valid, because the text starts with an h1 tag and ends with a non-matching h2 tag.
  2. Tags can be nested, but content between nested tags is considered not valid. For example, in <h1><a>contents</a>invalid</h1>, contents is valid but invalid is not valid.
  3. Tags can consist of any printable characters.

Input Format

The first line of input contains a single integer,N  (the number of lines).
The N subsequent lines each contain a line of text.

Output Format

For each line, print the content enclosed within valid tags.
If a line contains multiple instances of valid content, print out each instance of valid content on a new line; if no valid content is found, print None.

Sample Input

Sample Output

SOLUTION : –

SOLUTION : – 2 – Optimize Solution

To optimize your code, you can simplify the logic inside the while loop and remove unnecessary operations. Here’s the optimized version:

FOLLOW FOR MORE QUESTIONS AND SOLUTIONS | DIGIT WOOD

Leave a Reply

Your email address will not be published. Required fields are marked *