#Strings Common Prefix

1 messages · Page 1 of 1 (latest)

tender panther
#
public class Solution {
    public static String longestCommonPrefix(String[] strs) {
        int length_string = strs[0].length();
        String product = "";
        char reference = strs[0].charAt(0);

        for(int i = 0; i<length_string; i++){
            // one letter = 0th letter has been found
            for(int j = 0; j<strs.length; j++){
                System.out.println(reference);
                System.out.println(strs[j].charAt(i));
                if(reference != strs[j].charAt(i)){
                    return product;
                }
                product += reference;
                reference = strs[0].charAt(i+1);
                System.out.println(reference);
            }
        }
        
        return product;
    }
    
    public static void main(String[] args) {
        String[] strs = {"flower","flow","floght"};
        longestCommonPrefix(strs);
    }
}
lucid skiffBOT
# tender panther ```java public class Solution { public static String longestCommonPrefix(Str...

Detected code, here are some useful tools:

Formatted code
public class Solution {
  public static String longestCommonPrefix(String[] strs) {
    int length_string = strs[0] .length();
    String product = "";
    char reference = strs[0] .charAt(0);
    for (int i = 0; i < length_string; i++) {
      // one letter = 0th letter has been found
      for (int j = 0; j < strs.length; j++) {
        System.out.println(reference);
        System.out.println(strs[j] .charAt(i));
        if (reference != strs[j] .charAt(i)) {
          return product;
        }
        product += reference;
        reference = strs[0] .charAt(i + 1);
        System.out.println(reference);
      }
    }
    return product;
  }
  public static void main(String[] args) {
    String[] strs = {
      "flower", "flow", "floght"};
    longestCommonPrefix(strs);
  }
}
#

<@&987246399047479336> please have a look, thanks.

lucid skiffBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

tender panther
#

Above is the following code - where I'm trying to find what all prefixes are matching in each string. Keyword: Prefix

#

So the easy part is that all of it starts from the beginning of each string. Because the prefix needs to be same in all strings, I took the length of first string as my constant value, because further down the line, if there's a bigger word, it doesn't matter ... I'm realizing that I should've found the smallest word first ummm ... still the logic holds, even though I'll fix this part

Now, then I set the reference point of the code to be the first char of the first word... my output when I'm printing is the following ...

#
f
f
l
l
f
#

Idk why the reference is jumping back to an 'f'

tender panther
#

Someone please please or the channel will just close

quartz urchin
tender panther
#

Yeah

#

Wait I just came up with an update

#
package practice;

public class Solution {
    public static String longestCommonPrefix(String[] strs) {
        int length_string = strs[0].length();
        String product = "";
        char reference = strs[0].charAt(0);

        for(int i = 0; i<length_string; i++){
            // one letter = 0th letter has been found
            for(int j = 0; j<strs.length; j++){
                if(reference != strs[j].charAt(i)){
                    return product;
                }
            }
            product += reference;
            reference = strs[0].charAt(i+1);
        }
        
        return product;
    }
    
    public static void main(String[] args) {
        String[] strs = {"flower","flow","flight"};
        String solution = longestCommonPrefix(strs);
        System.out.println(solution);
    }
}
lucid skiffBOT
tender panther
#

I've been working on it and the issue is that I was changing the reference and the product inside the inner loop even though it should've been changed in the outer loop

#

SO that all SEEMS to be working fine, but I get an ArrayOutofBound error which clearly means, that the first word wasn't the smallest word, and because I run the program word1.length() times, it went to some word further down in the array and fucked it up

quartz urchin
tender panther
#

So see @quartz urchin I can run another loop prior to this whole saga to just determine the shortest word in this whole thing ... but like I don't wanna. Effiency-wise it's ASS SHIT

#

I'm so confused. Like I'm an engineering student. I can't just be brute-forcing my way through life...

#

So how can I tackle this efficiency stuff?! Please please help out!

tender panther
#

Hurts my ego as a programmer in terms of efficiency 😂 😂

#

Sorry for the rant, but I think you get the point

quartz urchin
tender panther
quartz urchin
#

you can try with the comparator interface

#
    String smallest = "";
    System.out.println("");
    for(String string :collegeArray){
        //if input-string is null or empty
        if(string == null || string.trim() == "" || string.trim().length()==0){
            System.out.println("Emtpy String Encountered");
            continue;
        }
        if(smallest == ""){
            smallest = string;
            continue;
        }
        if(string.length() < smallest.length()){
            smallest = string;
        }
    }
    return smallest;
}```
lucid skiffBOT
# quartz urchin ```public static String SmallestString(ArrayList <String> collegeArray){ Str...

Detected code, here are some useful tools:

Formatted code
public static String SmallestString(ArrayList<String> collegeArray) {
  String smallest = "";
  System.out.println("");
  for (String string : collegeArray) {
    //if input-string is null or empty
    if (string == null  || string.trim() == "" || string.trim().length() == 0) {
      System.out.println("Emtpy String Encountered");
      continue ;
    }
    if (smallest == "") {
      smallest = string;
      continue ;
    }
    if (string.length() < smallest.length()) {
      smallest = string;
    }
  }
  return smallest;
}
quartz urchin
#

This code is pretty good, uses a for cycle, which's the only way I can think of haha

#

just change the ArrayList for a normal String[]