Confusded on what I'm doing wrong in LC problem

I'm doing the Longest Common Prefix LC problem, and got to 66 tests passing out of 126.

One of these tests is "["flower","flow","flight"]", and it's supposed to get "fl" back, but my code gives "flo" back. I've been trying to slowly chip away at these edge cases, but everything I try makes less tests pass in the end. I'm sure there are going to be further issues even after fixing this case, of course.

Please, even if my code "concept" is bad, I want to keep going with it. I'm not gonna restart from the beginning. I'm just a beginner programmer that only programs as a hobby.

class Solution {
    public static String longestCommonPrefix(String[] strings) {
        String prefix = "";
        List<String> prefixStorage = new java.util.ArrayList<>(List.of());

        for (int i = 0; i < strings.length; i++) {
            int j = 0;
            while (j < strings[i].length()) {
                if (strings[i].isEmpty()) {
                    return "";
                } else if (strings[i].length() == 1) {
                    prefix = strings[i];
                } else if ((i < strings.length - 1) && (strings[i + 1].length() == 1)) {
                    prefix = strings[i + 1];
                } else if ((i < strings.length - 1) && (j < strings[i].length()) && (j < strings[i + 1].length()) && strings[i].startsWith(strings[i + 1].substring(0, j)) && strings[i].substring(0, j).length() > prefix.length()) {
                    prefix = strings[i].substring(0, j);
                }
                j++;
            }
            prefixStorage.add(prefix);
        }

        List<String> sortedPrefixStorage = new java.util.ArrayList<>(prefixStorage.stream()
                .sorted(Comparator.comparingInt(String::length))
                .toList());

        return sortedPrefixStorage.getFirst();
    }
}
reddit.com
u/Zeznon — 2 days ago

Code not giving the right results after refactoring for the website version

I'm just trying to to these challenges just because, in Java. I make the code in IntelliJ Idea in the way I want, then I refactor it for leetcode after it's giving correct results. Leetcode wants a class with a function inside, but I don't create any classes or functions, and just have the void main() {}.

The leetcode version always returns [0,0] for some reason. The array I created to get the return values out of the functions isn't updating for some reason, I guess. BTW, the target number is the last number in the array.

Working code:

void main() {
    String[] numberStringArray = IO.readln("Enter the array of integers, and the target: ")
            .replaceAll("[^\\d-,]", " ")
            .trim()
            .split(",");

    for (int i = 0; i < numberStringArray.length; i++) {
        numberStringArray[i] = numberStringArray[i].trim();
    }

    int[] numberArray = Arrays.stream(numberStringArray)
            .mapToInt(Integer::parseInt)
            .toArray();

    int i1 = 0;
    int i2 = 0;
    for (int i = 0; i < numberArray.length - 1; i++) {
        for (int j = 1; j < numberArray.length - 1; j++) {
            if (numberArray[i] + numberArray[j] == numberArray[numberArray.length - 1]) {
                i1 = i;
                i2 = j;
            }
        }
    }

    IO.println(String.format("[%d, %d]", i1, i2));
}

Leetcode website version

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int i1 = 0;
        int i2 = 0;
        for (int i = 0; i < nums.length - 1; i++) {
            for (int j = 1; j < nums.length - 1; j++) {
                if (nums[i] + nums[j] == target) {
                    i1 = i;
                    i2 = j;
                }
            }
        }
        int[] array = {0, 0};
        array[0] = i1;
        array[1] = i2;
        return array;
    }

    void main() {
        String[] numberStringArray = IO.readln("Enter the array of integers, and the target: ")
                .replaceAll("[^\\d-,]", " ")
                .trim()
                .split(",");

        for (int i = 0; i < numberStringArray.length; i++) {
            numberStringArray[i] = numberStringArray[i].trim();
        }

        int[] numberArray = Arrays.stream(numberStringArray)
            .mapToInt(Integer::parseInt)
            .toArray();

        int[] array = twoSum(numberArray, numberArray[numberArray.length - 1]);
        int i1 = array[0];
        int i2 = array[1];

        IO.println(String.format("[%d, %d]", i1, i2));
    }
}
reddit.com
u/Zeznon — 4 days ago

How to use regex to select all non-digits and non-minus sign?

I'm having trouble converting a working piece of code from Java to JS, involving regex. The Java regex is "[^\d|^-]". I haven't been able to make "/\D|^-/g" ignore minus signs.

The code itself is just a leetcode exercise.

reddit.com
u/Zeznon — 4 days ago

Confused on what Project Valhalla does to primitives and the corresponding objects

I've been reading about PV, but I'm not sure on what it means in some parts. Will we still need to deal with both "int" and "Integer", and not being able to use some methods with the other one? Like, (int) vs Integer.parseInt() and other strange things like that.

reddit.com
u/Zeznon — 5 days ago

What is wrong with this converted code?

I was watching Coding Train's tutorials, and stuff was working fine on the web editor in p5js, but, this happened when trying to convert to python in the IDE. Same for Java. JS says it can't install, so I can't test that one. I'm in Linux, CachyOS KDE.

u/Zeznon — 6 days ago

How to slow something down when it's too fast?

I'm new to this (p5.js and javascript for the most part), and was trying to make a background change color randomly, but it's just too damn fast (😵‍💫, felt weird for a while, didn't expect that). How do I slow it down so it only changes color every second?

reddit.com
u/Zeznon — 6 days ago
▲ 0 r/PTCGL

Where to find deck lists for PTCGL expanded?

I wanna give it a try, but I can only find deck lists for expanded proper.

Edit: Also for Trainer Trials. I can't find anything for it either.

reddit.com
u/Zeznon — 10 days ago
▲ 59 r/pkmntcg

Probably has been asked before, but why are Mega exes bad at the moment?

I've returned after 1~2 years, so I don't know much about previous metas. I do know that Mega exes were good before rotation or something, but now they're bad. What's the issue? Did something all these decks used rotate?

reddit.com
u/Zeznon — 16 days ago

I got curious after playing some stuff in MD Solo Mode

What year would you say some of these prebuilt decks' speed/power are closest to? They don't have the insane combos meta decks have, but it's faster than early yugioh.

I'm asking this because the speed reminds me of Pokemon TCG, just without the prizes and knockouts, and I've enjoyed it quite a bit.

reddit.com
u/Zeznon — 17 days ago
▲ 2 r/PTCGL

Coming back after leaving at Around Terapagos release

Apparently my account got reset? Oh well.

What are the changes since then? Like new modes, expanded changes, and what's the current best way to get resources for cards?

reddit.com
u/Zeznon — 23 days ago

How to import everything but an specific item from an inventory using Integrated Dynamics?

I have an ATM 10 save, where I got a Miner Spirit from Occultism, and I want to import everything but the lamp itself. I'm not sure what to do other than put everything it generates in a list variable.

reddit.com
u/Zeznon — 25 days ago

Packs for someone who likes processing lines but not autocrafting (think AE2)

I'm playing All the Mons right now, and I've just finished automating Mekanism's Fusion Reactor (I had never done it before), and really liked the while process of automation of resources needed for the Fission Reactor to run (Now I only need Fluorite and Uranium from "outside"). I had done the 5x process a few years ago too, just not in this save (I did Oritech this time). I did other stuff in other saves, it's usually not very long.

In the other hand, I have done a proper autocrafting system once, in Enigmatica 2 normal. I just never seem to have the patience for it.

It's an unfortunate personal issue I have with GregTech, while the processing lines look cool, it's actual hell without autocrafting or literally creating a process for everything, which is just as draining as autocrafting for me.

Is there something for me?

reddit.com
u/Zeznon — 29 days ago

Not enough mod books

Just started MeatballCraft and this is my default inventory.

u/Zeznon — 1 month ago

How do I make a tool from Silent gear using Productive Metalworks?

I'm playing All the Mons, and realized you need a smeltery to do Silent Gear stuff, so I built it, but the (example) iron didn't pour over the pickaxe head blueprint. How do you do it properly?

reddit.com
u/Zeznon — 1 month ago

How much RAM for All the Mons? And my system good enough? (see below)

I have 18GB of RAM installed in my laptop (20GB total, 2GB is dedicated, 4GB soldered + 16GB). Running Linux, CachyOS KDE.

It's a Ryzen 5 3500U with a Radeon Vega 8.

reddit.com
u/Zeznon — 1 month ago

Can you finish SF5 only on peaceful?

Started playing it, on peaceful (I dislike hostile mobs in a void world); and I'm having some difficulty getting some dyes. I saw that you can get light gray dye from a l.g. creeper on any biome, but I can't find other ways so far (for cyan dye too, for example).

reddit.com
u/Zeznon — 1 month ago
▲ 3 r/MAME

How to configure controls correctly in Daytona USA?

I was testing it in 0.287 (Runs well so far; might have missed some visual glitch), and after assigning the controls, I've realized that when moving the cursor to the right it moves back to the left if I stop holding the analog to the right. Also, the ai keeps passing me, so maybe I'm not going at full speed, or turning is losing too much speed?

reddit.com
u/Zeznon — 2 months ago