This repository acts as a personal archive for my solutions to EdX course *Data Structures and Software Design* from PennX.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

34 lines
992 B

import java.util.Queue;
import java.util.Stack;
/*
* SD2x Homework #2
* Implement the method below according to the specification in the assignment description.
* Please be sure not to change the method signature!
*/
public class HtmlValidator {
public static Stack<HtmlTag> isValidHtml(Queue<HtmlTag> tags) {
Stack<HtmlTag> matchedTags = new Stack<HtmlTag>();
while(!tags.isEmpty()){
HtmlTag tag = tags.remove();
if(tag.isSelfClosing()){
continue;
}
if(tag.isOpenTag()){
matchedTags.push(tag);
}else{
if(matchedTags.isEmpty()){ return null; }
if(tag.matches(matchedTags.peek())){
matchedTags.pop();
}else{
if( matchedTags.isEmpty() ){ return null; }
break;
}
}
}
return matchedTags;
}
}