-
Notifications
You must be signed in to change notification settings - Fork 0
/
java16.java
47 lines (41 loc) · 1.56 KB
/
java16.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//hackerrank
//Tag Content Extractor problem
//solution
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class TagContentExtractor{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int testCases = Integer.parseInt(in.nextLine());
String regexPatern = "(<[^>]*>)";
Pattern stringPatern = Pattern.compile(regexPatern);
while(testCases>0){
String line = in.nextLine();
int pos = 0;
Matcher m = stringPatern.matcher(line);
String previousHTMLTag = null;
int previousTagPos = -1;
boolean didFind = false;
while(m.find())
{
String htmlTag = line.substring(m.start(),m.end());
if(htmlTag.charAt(1) != '/')
{
previousHTMLTag = htmlTag;
previousTagPos = m.end();
}else if(htmlTag.charAt(1) == '/' && previousHTMLTag != null){
if(previousHTMLTag.substring(1).equals(htmlTag.substring(2))&&previousHTMLTag.length()>2 && m.start()>previousTagPos+1){
System.out.println(line.substring(previousTagPos,m.start()));
didFind = true;
}
previousHTMLTag = null;
}
}
System.out.print(didFind?"":"None\n");
testCases--;
}
}
}