|
| 1 | +package medium; |
| 2 | + |
| 3 | +import java.util.Stack; |
| 4 | +import java.util.regex.Matcher; |
| 5 | +import java.util.regex.Pattern; |
| 6 | + |
| 7 | +/** |
| 8 | + * Have the function HTMLElements(str) read the str parameter being passed |
| 9 | + * which will be a string of HTML DOM elements and plain text. |
| 10 | + * --- |
| 11 | + * The elements that will be used are: b, i, em, div, p. |
| 12 | + * For example: if str is "<div><b><p>hello world</p></b></div>" then this string of DOM elements |
| 13 | + * is nested correctly so your program should return the string true. |
| 14 | + * --- |
| 15 | + * If a string is not nested correctly, return the first element encountered where, |
| 16 | + * if changed into a different element, would result in a properly formatted string. |
| 17 | + * --- |
| 18 | + * If the string is not formatted properly, |
| 19 | + * then it will only be one element that needs to be changed. |
| 20 | + * For example: if str is "<div><i>hello</i>world</b>" |
| 21 | + * then your program should return the string div |
| 22 | + * because if the first <div> element were changed into a <b>, |
| 23 | + * the string would be properly formatted. |
| 24 | + */ |
| 25 | +public class HtmlElements { |
| 26 | + |
| 27 | + /** |
| 28 | + * A stack helps to determine if elements are nested correctly. |
| 29 | + */ |
| 30 | + private static final Stack<String> stack = new Stack<>(); |
| 31 | + |
| 32 | + /** |
| 33 | + * Process a string to prepare for further inspection. |
| 34 | + * 1. convert to lowercase |
| 35 | + * 2. remove text content between tags |
| 36 | + * 3. add spaces between tags |
| 37 | + * 4. remove multiple spaces |
| 38 | + * 5. trim (remove leading and trailing spaces) |
| 39 | + * 6. split to array of strings |
| 40 | + * |
| 41 | + * @param str input string |
| 42 | + * @return parsed string |
| 43 | + */ |
| 44 | + private static String[] parseElements(String str) { |
| 45 | + return str |
| 46 | + .toLowerCase() |
| 47 | + .replaceAll(">[^<]+", ">") |
| 48 | + .replaceAll("([>])(?=[<])", "$1 ") |
| 49 | + .replaceAll(" +", " ") |
| 50 | + .trim().split(" "); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Checks if a string is an opening tag. |
| 55 | + * |
| 56 | + * @param tag a string to check |
| 57 | + * @return true if a string is an opening tag |
| 58 | + */ |
| 59 | + private static boolean isOpeningTag(String tag) { |
| 60 | + Pattern pattern = Pattern.compile("<[a-z]>|<[a-z][a-z1-9]+>"); |
| 61 | + Matcher matcher = pattern.matcher(tag); |
| 62 | + return matcher.find(); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Checks if a string is an closing tag. |
| 67 | + * |
| 68 | + * @param tag a string to check |
| 69 | + * @return true if a string is a closing tag |
| 70 | + */ |
| 71 | + private static boolean isClosingTag(String tag) { |
| 72 | + Pattern pattern = Pattern.compile("</[a-z]>|</[a-z][a-z1-9]+>"); |
| 73 | + Matcher matcher = pattern.matcher(tag); |
| 74 | + return matcher.find(); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Get an enclosed value. |
| 79 | + * |
| 80 | + * @param tag input tag with angle brackets |
| 81 | + * @return the enclosed value of a tag |
| 82 | + */ |
| 83 | + private static String getTagValue(String tag) { |
| 84 | + return tag.replaceAll("[></]", ""); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * HTML Elements function. |
| 89 | + * |
| 90 | + * @param str input string |
| 91 | + * @return "true" if elements are nested correctly, or an enclosed tag value if not. |
| 92 | + */ |
| 93 | + private static String htmlElements(String str) { |
| 94 | + String[] parsedTags = parseElements(str); |
| 95 | + for (String tag : parsedTags) { |
| 96 | + if (isOpeningTag(tag)) { |
| 97 | + stack.push(tag); |
| 98 | + } else if (isClosingTag(tag) && !stack.isEmpty()) { |
| 99 | + if (getTagValue(stack.peek()).equals(getTagValue(tag))) { |
| 100 | + stack.pop(); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + return stack.isEmpty() ? "true" : getTagValue(stack.peek()); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Entry point. |
| 109 | + * |
| 110 | + * @param args command line arguments |
| 111 | + */ |
| 112 | + public static void main(String[] args) { |
| 113 | + String result1 = htmlElements("<div><b><p>hello world</p></b></div>"); |
| 114 | + System.out.println(result1); |
| 115 | + String result2 = htmlElements("<div><b><p>hello world</b></div>"); |
| 116 | + System.out.println(result2); |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments