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);
}
}
#Strings Common Prefix
1 messages · Page 1 of 1 (latest)
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.
While you are waiting for getting help, here are some tips to improve your experience:
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.
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'
Someone please please or the channel will just close
you're printing reference twice, aren't you?
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);
}
}
Detected code, here are some useful tools:
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
yeah I was trying to solve the shortest string thing
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!
Asymptotic analysis wise, it's still O(n^2) total, but like I don't wanna make computer do extra stuff
Hurts my ego as a programmer in terms of efficiency 😂 😂
Sorry for the rant, but I think you get the point
I can't figure out another way to do it
Yeah same
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;
}```
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;
}