+ 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
20 Réponses
+ 4
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 🤦
+ 4
It works👏👏🥳
+ 3
So in the loop at 22 strtok is not working 🤔🤔🤔
+ 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
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
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
hey guys
+ 1
// Hello where. Here is My version on C#. it pass all 6 tests:
https://www.sololearn.com/ru/compiler-playground/c81zxq1Buzf4
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
string[] words = Console.ReadLine().Split(' ');
Array.Sort(words, (x, y) => x.Length.CompareTo(y.Length));
string word = words[0];
int length = word.Length;
for (int i = length; i > 0; i--)
{
HashSet<string> substrings = new HashSet<string>();
for (int j = 0; j <= length - i; j++)
{
substrings.Add(word.Substring(j, i));
}
List<string> ok = substrings.Where(ss => words.Skip(1).All(w => w.Contains(ss))).OrderBy(s => s).ToList();
if (ok.Any())
{
Console.WriteLine(ok[0]);
break;
}
}
}
}
0
NotAPythonNinja thanks 🤗
After reading your comment I tried a printf after the strcpy at line 45 to print sub and I saw that the program was printing "sololear" missing the "n", so I added the +1 in the strncpy.
However this wasn't the main problem 😔
It was just a bug I found thanks to you
- 1
Sample Input:
SoloLearn Learning LearningIsFun Learnable
Sample Output:
Learn
Explanation:
Learn is the longest common substring for the words SoloLearn Learning LearningIsFun Learnable.