Longest common substring code coach | SoloLearn: Learn to code for FREE!

+1

Longest common substring code coach

I am trying to solve the code coach "Longest common substring". I wrote a code that in my mind is supposed to work but it prints "{" that is how I initialized the array where I store the substring to output. I choose "{" because it comes after "z" as ASCII value. I can see no bug, please help me.🙏 I filled the code with comments to make it easy to read and left a sample input to copy/paste at the top of the code. EDIT: now the code works, but fails the last test https://code.sololearn.com/c4JRT6EmYVIq/?ref=app

11/22/2020 8:35:26 PM

Davide

23 Answers

New Answer

+2

Davide Crazy thought: what if the very last test case the common substring is one char long? I tried this, and your code prints {

+4

It works👏👏🥳

+3

So in the loop at 22 strtok is not working 🤔🤔🤔

+3

Omg of course 🤦 I need a do while. The condition is for word[w] but I initialize word[w] into the loop. So the condition is not met since the very first time 🤦

+3

The string that you posted wasn't handled properly because x is the first letter of the first word and the loop with k till k>j doesn't acces the first letter (k=0)

+3

Liked this one so much I typed up a quick solution in Python: https://code.sololearn.com/cCxE5xFSY514/?ref=app

+3

NotAPythonNinja nice job 👌 I:ll try to learn Python from your code😃🤗 It's nice to see how short is the code in Python

+1

NotAPythonNinja yes it works for the sample string that I gave. But it fals half of the tests in the code coach 😞🤔 I am going to copy the one that I can see

+1

NotAPythonNinja I tried the one failed that I can see and here it worked😅 I was missing the +1 in strncpy in the code of the code coach. Now the program fails only the last test and I can't see it 😭

+1

NotAPythonNinja omg you are right 😱😱😱

+1

NotAPythonNinja I completed the code coach 🎉🥳 Thank you so much for the help 🤗🤗🤗 The problem is that the for with i and j run till i or j < len so if len is 1 they are not entered. And the for with k should run till k>=j for the same reason. edit: the loops for i and j were actually right. The one with k was the only bugged one

+1

I tried it for you and it fails half of the tests. I can see one of them: funny funeral funfair

+1

A one-liner solution!?😱 That's a job for Python ninjas 🤣

+1

There were 2 more that I can't see but probably are for the same reason

+1

NotAPythonNinja 14 lines!?😱😱 You are on the way to become a python ninja🤣

+1

Davide I had a go at translating my Python solution back into C, and arrived at this monstrosity: Well, it works. 😁😂 https://code.sololearn.com/cxsq0hr27KrV/?ref=app

+1

NotAPythonNinja I tried it for you and it passes all the tests! 🎉🥳🥳🥳 However I tried entering 3 words of 2 letters like fg fg fg and it doesn't work properly. And you can remove the while at the beginning to take off new lines because fgets strops at the first \n, it doesn't read multiple lines

+1

No don't worry about that, strtok will ignore the new line at the end.

+1

NotAPythonNinja 🥳you are very welcome 🤗 It's nice to have people with whom to share and to test codes🤗

+1