leetcode like problem? optimization of nesting tags

i made a program to make text look spooky in html which is achieved by randomly applying a style tag to every single character however, the problem is that it really quickly balloons in character count and that's a problem for me
No description
No description
3 Replies
JavaBot
JavaBot4w ago
This post has been reserved for your question.
Hey @2ndchar! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
2ndchar
2ndcharOP4w ago
public static boolean[] mundaneCSS = new boolean[7];

public static void main(String[] args) {

System.out.println("Input text to be transformed");
String textInput = input.nextLine();
char[] charInput = textInput.toCharArray();


boolean[] previousMundaneCSS = mundaneCSS.clone();

Stack<String> openTags = new Stack<>();

int totalCounter = 0;
for (char c : charInput) {
randomlySwitchCSS();
for (int i = 0; i < mundaneCSS.length; i++) {
if (mundaneCSS[i] && (c != ' ')) {
String tag =
switch(i) {
case 0 -> "b"; // bold
case 1 -> "i"; // italics
case 2 -> "u"; // underline
case 3 -> "s"; // strikethrough
case 4 -> "sub"; // subscript
case 5 -> "sup"; // superscript
case 6 -> "code"; // code
default -> "span";
};

System.out.print("<" + tag + ">");
totalCounter += tag.length() + 2;
openTags.push(tag);
}
}
System.out.print(c);
previousMundaneCSS = mundaneCSS.clone();

while (!openTags.isEmpty()) {
System.out.print("</" + openTags.pop() + ">");
}
}
System.out.println("\n" + totalCounter);

}
public static short counter = 0;

public static void randomlySwitchCSS() {
for(int i = 0; i < mundaneCSS.length; i++) {
if (Math.random()> 0.5) mundaneCSS[i] = !mundaneCSS[i];
}
counter++;
}
public static boolean[] mundaneCSS = new boolean[7];

public static void main(String[] args) {

System.out.println("Input text to be transformed");
String textInput = input.nextLine();
char[] charInput = textInput.toCharArray();


boolean[] previousMundaneCSS = mundaneCSS.clone();

Stack<String> openTags = new Stack<>();

int totalCounter = 0;
for (char c : charInput) {
randomlySwitchCSS();
for (int i = 0; i < mundaneCSS.length; i++) {
if (mundaneCSS[i] && (c != ' ')) {
String tag =
switch(i) {
case 0 -> "b"; // bold
case 1 -> "i"; // italics
case 2 -> "u"; // underline
case 3 -> "s"; // strikethrough
case 4 -> "sub"; // subscript
case 5 -> "sup"; // superscript
case 6 -> "code"; // code
default -> "span";
};

System.out.print("<" + tag + ">");
totalCounter += tag.length() + 2;
openTags.push(tag);
}
}
System.out.print(c);
previousMundaneCSS = mundaneCSS.clone();

while (!openTags.isEmpty()) {
System.out.print("</" + openTags.pop() + ">");
}
}
System.out.println("\n" + totalCounter);

}
public static short counter = 0;

public static void randomlySwitchCSS() {
for(int i = 0; i < mundaneCSS.length; i++) {
if (Math.random()> 0.5) mundaneCSS[i] = !mundaneCSS[i];
}
counter++;
}
i've asked for help with this problem elsewhere, and someone made the observation that i can manually cut down on the tags by deleting any tags that match the pattern of </??><??> it seems to me that i need to save the previous character's tags first and then check the current tags against the first one and print accordingly for example (to cut down on complexity, pretend the numbers represent tags like <b>, and the numbers just represent their correct order) if the previous character's tags was 1 2 3 4 5 and the current character's tag was 1 2 3 so, unoptimized, it would look like 1 2 3 4 5 (A) 5 4 3 2 1 1 2 3 (B) 3 2 1 optimized, it would look like 1 2 3 4 5 (A) 5 4 (B) 3 2 1 one approach i haven't tried yet is looking at this problem literally step by step at (A), the program knows it has tags 1 2 3 4 5 since it's at the beginning, it prints all of 1 2 3 4 5 then it looks at the next letter, (B), and its tags are 1 2 3 i see that 4 and 5 is missing, so i print out 5 and 4 and then i look at the next letter and see that there's none so i print out 3 2 1
JavaBot
JavaBot4w ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?