2ndchar
2ndchar
JCHJava Community | Help. Code. Learn.
Created by 2ndchar on 4/4/2025 in #java-help
leetcode like problem? optimization of nesting tags
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
6 replies
JCHJava Community | Help. Code. Learn.
Created by 2ndchar on 4/4/2025 in #java-help
leetcode like problem? optimization of nesting tags
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++;
}
6 replies
JCHJava Community | Help. Code. Learn.
Created by 2ndchar on 4/10/2024 in #java-help
reader.readLine() not working as expected
thx for that
10 replies
JCHJava Community | Help. Code. Learn.
Created by 2ndchar on 4/10/2024 in #java-help
reader.readLine() not working as expected
wait i think thats it actually
10 replies
JCHJava Community | Help. Code. Learn.
Created by 2ndchar on 4/10/2024 in #java-help
reader.readLine() not working as expected
oh wait is it because of the while loop, whenever the for loops end, it runs again because of the while loop?
10 replies
JCHJava Community | Help. Code. Learn.
Created by 2ndchar on 4/10/2024 in #java-help
reader.readLine() not working as expected
uhh are you saying that i dont need two for loops?
10 replies