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

  1. import java.util.Queue;
  2. import java.util.Stack;
  3. /*
  4. * SD2x Homework #2
  5. * Implement the method below according to the specification in the assignment description.
  6. * Please be sure not to change the method signature!
  7. */
  8. public class HtmlValidator {
  9. public static Stack<HtmlTag> isValidHtml(Queue<HtmlTag> tags) {
  10. Stack<HtmlTag> matchedTags = new Stack<HtmlTag>();
  11. while(!tags.isEmpty()){
  12. HtmlTag tag = tags.remove();
  13. if(tag.isSelfClosing()){
  14. continue;
  15. }
  16. if(tag.isOpenTag()){
  17. matchedTags.push(tag);
  18. }else{
  19. if(matchedTags.isEmpty()){ return null; }
  20. if(tag.matches(matchedTags.peek())){
  21. matchedTags.pop();
  22. }else{
  23. if( matchedTags.isEmpty() ){ return null; }
  24. break;
  25. }
  26. }
  27. }
  28. return matchedTags;
  29. }
  30. }