forked from animesh0353/Hackerrank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Detect HTML Tags
46 lines (41 loc) · 1.33 KB
/
Detect HTML Tags
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
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Solution{
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner in = new Scanner(System.in);
int n = Integer.parseInt(in.nextLine());
String string;
String regexOnlyTag = "[<][^>]+";
String regexATag = "[\\[]";
Pattern tagPattern = Pattern.compile(regexOnlyTag);
Set<String> tagSet = new HashSet<String>();
for (int i = 0; i < n; i++)
{
string = in.nextLine();
Matcher tagMatcher = tagPattern.matcher(string);
while(tagMatcher.find())
{
String temp = string.substring(tagMatcher.start(), tagMatcher.end());
temp = temp.trim();
temp = temp.replaceAll("[<|>|/]", "");
String[] temparr = temp.split(" ");
String tempNew = temparr[0].trim();
tagSet.add(tempNew);
}
if(string.contains("["))
tagSet.add("a");
}
// sort the map based on key field
Set<String> sortedSet = new TreeSet<String>(tagSet);
StringBuffer strbuff = new StringBuffer();
for(String item : sortedSet)
strbuff.append(item + ";");
strbuff.deleteCharAt(strbuff.length() - 1);
System.out.println(strbuff.toString());
}
}