Skip to content

Instantly share code, notes, and snippets.

@rfaisal
Last active December 20, 2015 12:09
Show Gist options
  • Select an option

  • Save rfaisal/6128628 to your computer and use it in GitHub Desktop.

Select an option

Save rfaisal/6128628 to your computer and use it in GitHub Desktop.
Given an array of html as strings, print all the tags separated by ; lexicographically. Omit duplicates.
public class HTMLTagsDetection {
public static String toStringLexicographically(String[] htmls){
HashSet<String> tags= new HashSet<String>();
for(int i=0;i<htmls.length;i++){
detect(tags, htmls[i]);
}
String[] sorted=new String[tags.size()];
tags.toArray(sorted);
Arrays.sort(sorted);
return implodeArray(sorted,";");
}
private static void detect(HashSet<String> tags, String html){
for(int i=0;i<html.length();i++){
if(html.charAt(i)=='<'){
i++;
StringBuilder sb= new StringBuilder();
while(i<html.length() && html.charAt(i)==' ') i++;
while(i<html.length() && html.charAt(i)!=' ' && html.charAt(i)!='>' && html.charAt(i)!='/'){
sb.append(html.charAt(i));
i++;
}
if(sb.length()!=0)
tags.add(sb.toString());
}
}
}
public static String implodeArray(String[] inputArray, String glueString) {
if (inputArray.length > 0) {
StringBuilder sb = new StringBuilder();
sb.append(inputArray[0]);
for (int i=1; i<inputArray.length; i++) {
sb.append(glueString);
sb.append(inputArray[i]);
}
return sb.toString();
}
return "";
}
}
public class HTMLTagsDetectionTest {
@Test
public void testToStringLexicographically() {
assertEquals("a;div;p", HTMLTagsDetection.toStringLexicographically(new String[]{"<p><a href=\"http://www.quackit.com/html/tutorial/html_links.cfm\">Example Link</a></p>","<div class=\"more-info\"><a href=\"http://www.quackit.com/html/examples/html_links_examples.cfm\">More Link Examples...</a></div>"}));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment