Java Tag Content Extractor – Hacker Rank Solution

Java Tag Content Extractor – Hacker Rank Solution

Hello Friends, How are you? Today I am going to solve the HackerRank Java Tag Content Extractor Problem with a very easy explanation. This is the 24th problem of Java on HackerRank. In this article, you will get more than one approach to solving this problem. So let’s start- {tocify} $title={Table of Contents} In a tag-based language like XML or HTML, contents are enclosed between a start tag and an end tag like contents. 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 the same. The HTML code

    Hello World

    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


    , contents is valid but invalid is not valid.

  3. Tags can consist of any printable characters.

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

  • Each line contains a maximum of 10^4 printable characters.
  • The total number of characters in all test cases will not exceed 10^6.
  • 1 0) { String line = in.nextLine(); Matcher tagMatcher = tagPattern.matcher(line); if (tagMatcher.find()) { do { System.out.println(; } while (tagMatcher.find()); } else { System.out.println(“None”); } testCases–; } } } Approach III:
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*; public class Solution{ public static void main(String[] args){ Pattern pattern = Pattern.compile("]+)>([^
No Comments

Sorry, the comment form is closed at this time.