help with a leetcode problem
Trying to complete leetcode 14 and keep running into the DEADLYSIGNAL error. I know the code is very messy and brute force but I am trying to do it without copy pasting solutions.


Solution:Jump to solution
so yeah, personally, just to avoid any out of bounds index issues, my process would
- sort by shortest string
- set a string that you update with the shortest[0]->shortest[i]
- iterate through the strings horizontally first
- iterate through the array vertically. comparing the strings to shortest at the index of the first loop...
43 Replies
@sidge if you want to take a look at it.
I looked up DEADLYSIGNAL and im assuming it cause of an "out-of-bounds access" which I tried to fix with the IF statement inside of the while loop but it didn't change anything.
also I can already see I messed up the prefix creation.
is that "while" supposed to have a single = sign?
Yeah I’m not a c++ guy so idk that error
I also don’t understand the while loop nested inside the two for loops
(oh yeah I didn't even see that, I just looked at the error message then checked line 14)
it's a very brute force method so I was doing
first for loop is word 1
second for loop is word 2
while loop will check if the characters at matching index are the same and then add them to a prefix which will be check to see if its longer than the longest prefix (if there is one from before).
if that makes any sense.
also I understand its a very brute force method.
just noticed my increments for the while loop are in the wrong spot but I feel like there is bigger issues as well.
What I would do, granted this is coming from someone who hasn’t done this problem in a while, is finding the shortest string in the array, and using that as the length of the second for loop, compare all the strings to the value at that index of the shortest, and if it passes all, add that character to a string or make the string you’re storing a slice to that index, and if not, just return the stored string from the previous index
If you sort the string array you can also shave off one iteration from the nested for loop because you already know the 0th index string is the shortest
Oh I think my brain was overcomplicating this problem as I was writing my explanation
Though that is totally marginal gains
yea, I don't think my mess of code helped :Despairge:
Yeah it’s not a crazy problem but it definitely can trip you up if you think too hard
I was thinking that it was a problem involving any of the strings in the array having potential matching prefixes with any other string, not prefixes that are common with every string in the array
All you need is a record of the string up until the first for loop index until it fails
Yeah
Or ig second for loop depending on how you’re doing it
-# I have never done leetcode and given everyone's reactions to leetcode I was expecting something more like this
Leetcode is absolutely all over the place. Some are very easy, and easys are very hard
By the way is this required to be done in C++ because that really does add extra difficulty for basically no reason. More understanding C++ at that point then understanding the problem itself.
no but I am ironically most comfortable with C++ since its what I used during most of my classes in college.
Fair enough
Does my explanation make sense?
kinda, imma just throw my current code block in here with comments/pseudo code so maybe you could make since of my thinking.
Damn I’m on my phone rn, I’ll check when I’m home
but I do think I understand what you were saying.
But the third loop is totally unnecessary
I am looking at this, one of these for loops seems unnecessary (I think sidge's explanation kind of implied that but I am looking more at the code you have then his breakdown)
Darnit man typing faster then me :/
lol
Yeah you basically only need to take the first string, compare that to every other string,
Then if you run into the end of the shortest string you can end there.
I don't think you even need to find the shortest string, just break the loops if you finish comparing the first string to that?
(or break the loop if the first string is gone through)
My question there would be what if there is a prefix longer than the shortest string?
No based on how this question is worded
Since this is a common prefix amongst all strings from my understanding.
therefore it can't be a "common" prefix if it's the entirity of the shortest string
If this was the problem I was thinking it was, which is the longest common prefix among any possible words in an array (IE you could be tracking multiple potential prefixes to find the longest) that would be different.
oh, that makes sense. I thought we were looking for the longest prefix between two elements in the vector, not every element in the vector.
🤦♂️
Other minor potential bugs in your code, I feel like there is a case where you get an out of bounds but that kind of depends on how ++i works compared to i++ since that may cause slightly different behavior
yea, I noticed that as well.
especially with the 2nd for loop since it was always looking for the next index without checking if the next index was even there.
Is that even possible? They all have to share the prefix
oh yeah, important distinction
I am mostly mentally drafting the behavior which you should really do with a pen and paper (trust me this helps a lot for problems like this, write out the behavior of your loops)
But it looks like you will try and go to the last item in the array, and compare it to the last item + 1 in the array given how i and k are working.
Which for C++ will throw an error
If doing this (which we are not) my first thought would be just compare the first letters of each string, break take strings with similar first letters,
Then compare the second letter in those strings with similar first letters, break into strings with similar second letters, (ect)
then track how far you get
Likely a recursive function in C++
Solution
so yeah, personally, just to avoid any out of bounds index issues, my process would
- sort by shortest string
- set a string that you update with the shortest[0]->shortest[i]
- iterate through the strings horizontally first
- iterate through the array vertically. comparing the strings to shortest at the index of the first loop
- this is so, if you can visualize, you can get a vertical "slice" of it all to compare
- have a return clause the moment something is not equivalent
- at the end of the top level for loop. append that character value to your result string
take it with a grain of salt, as I am explaning this without writing any code
(granted at this point you are practically just trying to find the 2 elements most similar to each other)
:NOTED:
granted you don't actually need to sort, you can just fidn the shortest and leave everything as is, and just iterate with that length as the constraint for your for loop
I used a combination of the shortest word and just iterating to do it from scratch. Here was my result if you guys are interesting in seeing what it looked like completed.
Lookin good buddy. Does it pass the leetcode tests and all?
Hopefully now that you've got it done, it makes a lot more sense! On one hand, I'm surprised I remembered the solution off the cuff like that, but, on the other hand, once you realize how simple the solution actually is (not to diminish the effort you put in, trust me we've all been there), it kinda sticks and you don't really forget
Yea, I am in a weird spot where it feels like I am “relearning” everything cause while I have my associates degree, allot of the classes were very hand holdy and didn’t require you to completely build something without being told what or how to do it.
I am not going to lie, looking at these problems I would argue the definitions of what it wants you to do are quite bad.
The reality of being a GOOD coder is not being able to translate terrible written requirements, it’s asking whoever gave you those requirements to clarify and ask them questions to build a better understanding of what they want.
If interviewers who use Leetcode don’t let you do this, then it is a terrible interview tool as you are basically training coders to make assumptions rather than build clarification.
yeah leetcode has never been about making industry-quality, like good code
it's brainteasers
that's all its ever been, and all it ever will be
best practices are out of the question