Coding challenge longest substring contained in two strings

Coding Challenge Coding Live

Find the longest repeating character in a sorted string

One caveat: No C, C, C++, Javascript or anyC-based programming language^], nor any VB-based language (VB, , VBScript). That cuts out most of the easy languages. Spread your wings!

String containing substring problem

This solution was built using theCoding Ground^] online IDE. Here is a link tothe solution^] where you can compile and run it.

This content, along with any associated source code and files, is licensed underThe Code Project Open License (CPOL)

But I would have to implement HashSet and Dictionary myself and I dont think Im up to that at this time.Graeme_Grant29-Jan-17 21:25pmPascals type StrDict: Array of Array of String;… Then, to use: var DictName: StrDict; ? But then again, Im only new to the language…

Enjoy! :)PermalinkPosted28-Jan-17 12:39pmGraeme_Grant48.3KUpdated28-Jan-17 12:45pmv2CommentsBryian Tan28-Jan-17 18:51pmTwo solution from Graeme_Grant!!! Look like you are the only participant !!!! 🙂 anyway congrats on your previous two competitions. You done a great job. I think you getting this one too.Graeme_Grant28-Jan-17 18:53pmThanks Bryian!

Coding challenge: longest substring contained in two strings

PIEBALD.Type.Substring.EnumSubstrings ( Subject [ i ] , shortest ) ) System.Collections.Generic.HashSetint v ;

The PIEBALD.Type.Substring class is similar to the StringSlice class I used for the Bad Word Filter challenge, but with more bells and whistles.

C program for length of the longest run in the string

I have VAX BASIC, HP BASIC, and Turbo BASIC…PIEBALDconsult27-Jan-17 18:45pmWell, how are BCPL and B considered C-like? They came first! (And second.)PIEBALDconsult27-Jan-17 18:19pmI think I have Pascal on one of my AlphaServers. I have a license. I also have a license for COBOL…Graeme_Grant28-Jan-17 5:28amThis may help:Free Turbo Pascal for Windows 10^] – have not tried it thought…PIEBALDconsult28-Jan-17 10:04am:cough: Win 7 :cough:Graeme_Grant28-Jan-17 11:27amIt is amazing what you can find with Google when one uses it… 😉

ProgramLongestSubstringContained(output);typedynStrings =arrayofstring; procedure QuickSort(input : dynStrings; lowPos, highPos : integer);varmovablePointer, immovablePointer, temporaryPointer :integer; temporaryItem :string;beginimmovablePointer := lowPos; movablePointer := highPos;while(movablePointer immovablePointer)dobeginif(movablePointer immovablePointer)thenbeginif(input[movablePointer] input[immovablePointer])thenbegintemporaryItem := input[movablePointer]; input[movablePointer] := input[immovablePointer]; input[immovablePointer] := temporaryItem; temporaryPointer := movablePointer; movablePointer := immovablePointer; immovablePointer := temporaryPointer;endelsebegindec(movablePointer);end;endelsebeginif(input[movablePointer] input[immovablePointer])thenbegintemporaryItem := input[movablePointer]; input[movablePointer] := input[immovablePointer]; input[immovablePointer] := temporaryItem; temporaryPointer := movablePointer; movablePointer := immovablePointer; immovablePointer := temporaryPointer;endelsebegininc(movablePointer);end;end;end; if(movablePointer lowPos)thenQuickSort(input,lowPos,movablePointer-1); if(movablePointer highPos)thenQuickSort(input,movablePointer+1,highPos);end;//remove blank array entries…functionTrimArray(input: dynStrings) : dynStrings;vari, len, count:integer; ret: dynStrings;begincount :=0; len := Length(input);fori :=0tolendobeginif(Length(input[i]) 0)thenbegincount := count +1; setlength(ret, count); ret[count -1] := input[i];end;end; TrimArray := ret;end;functionRetrieveBestResult(strings :dynStrings) :string;varstr, tmp:string; strLen, strCount, i, len, tmpCount:integer;begintmpCount :=0; strCount :=0; strLen :=0; tmp :=; str :=;//order the arrayQuickSort(strings, low(strings), high(strings));//find the most popular longest stringfori :=0tohigh(strings)dobeginlen := length(strings[i]);if(len = strLen)thenbeginstrCount :=1; str := strings[i]; strLen := len;endelseif(str = strings[i])thenstrCount := strCount +1elseif(tmp = strings[i])thentmpCount := tmpCount +1elsebegintmp := strings[i]; tmpCount :=1;end;end; RetrieveBestResult := str;end;//check for a matchfunctionStrInArray(value :string; strings :dynStrings) :Boolean;varstr :String;beginforstrinstringsdobeginiflength(value) =0thenexit(false);//if (lowercase(value) = lowercase(str)) thenif(value = str)thenbeginexit(true);end;end; StrInArray := false;end;functionProcess(input: dynStrings) :string;varmatches: dynStrings; allMatches: dynStrings; i, c, cc, count, len:integer; str, sstr:string;beginsetlength(allMatches, 0); setlength(matches, 0);fori :=0tohigh(input)dobeginstr := input[i]; count :=0; len := length(str);forc :=0tolendobeginforcc :=1tolen – cdobeginsstr := copy(str, c, cc);if(high(allmatches) = -1) or (StrInArray(sstr, allMatches))thenbegincount := count +1; setlength(matches, count); matches[count -1] := sstr;end;end;end;//bounce early if no matchesif(high(matches) 1)thenexit(no match); allMatches := copy(matches,0, length(matches)); writeln(Matches found: , high(allMatches)); setlength(matches, 0);end; Process := RetrieveBestResult(allMAtches);end;functionGetLCS(input: dynStrings) :string;varcount:integer; work: dynStrings;begin//check and cleancount := Length(input);if(count = 0)thenexit(empty)elsebeginwork := TrimArray(input); count := Length(work);if(count = 0)thenexit(empty);end;//process if work to be done…writeln(processing…); GetLCS := Process(work);end;vartests:array[0..4] ofstring;result:string;begintests[0] :=Yes, the classic LCS problem. Given two or more strings find the longest common substring in each string.; tests[1] :=; tests[2] :=One caveat: No C, C, C++, Javascript or any C-based programming language[^], nor any VB-based language (VB, , VBScript). That cuts out most of the easy languages. Spread your wings!; tests[3] :=; tests[4] :=Your solution should take into account null strings and strings whose length is limited only by available storage.;result:= GetLCS(tests); writeln(Longest Substring: , result);end.

System.Collections.Generic.HashSetint() ; temp [ s ].Add (

Okay, as F is acceptable, here is an F version that will accept multiple inputs to be compared and return the Longest common substring. I am using the challenges text (including nulls) as the input for testing… 😉

Find matching substring from two strings

Dont tell someone to read the manual. Chances are they have and dont get it. Provide an answer or move on to the next question.

Lets work to help developers, not make them feel stupid.

sh-4.3$ fpc -vw main.pas Free Pascal Compiler version 2.6.4 [2015/06/17] for x86_64 Copyright (c) 1993-2014 by Florian Klaempfl and others Target OS: Linux for x86-64 Compiling main.pas Linking main /usr/bin/ld: warning: link.res contains output sections; did you forget -T? 209 lines compiled, 0.1 sec sh-4.3$ main processing… Matches found: 5564 Matches found: 253 Matches found: 147 Longest Substring: ings sh-4.3$

As if F was not as far away from normal for me (second attempt ever), here is a FreePascal version for PIEBALDConsultant. This is my first attempt at programming with this language, so please be gentle as I am sure there is plenty of room for improvement…

( kvp.Value.Count == Subject.Length ) result.Add ( kvp.Key.ToString() ) ;

We were not given anything to work with, so yes, it was a block of text, not just a bunch of strings in an array. The challenges own text appeared to be the best candidate. This allows for any number of strings to be used, not just two (2). The first thing I do is convert it into a string array.

and fix the problem. Insults are not welcome.

( System.Collections.Generic.KeyValuePairPIEBALD.Type.Substring,System.Collections.Generic.HashSetint kvp

System.Collections.Generic.DictionaryPIEBALD.Type.Substring,System.Collections.Generic.HashSetint() ;

Graeme_Grant again gets the guernsey for his outstanding and, Im assuming, time consuming treatment of last weeks problem. Noice.Posted27-Jan-17 4:25amUpdated5-Feb-17 16:41pmAdd a SolutionCommentsGraeme_Grant27-Jan-17 10:36amThanks Chris. But not really … tinkering while watching a movie…

Dynamically parsing substrings out of strings

[] Subject ) System.Collections.Generic.Liststring result =

Melatonin for jetlag but it doesnt work.

Understand that English isnt everyones first language so be lenient of bad spelling and grammar.

PIEBALD.Type.Substring.EnumSubstrings ( Subject [

———————————————————————– Input strings (including nulls / blank lines): Yes, the classic LCS problem. Given two or more strings find the longest common substring in each string. One caveat: No C, C, C++, Javascript or any C-based programming language[^], nor any VB-based language (VB, VB.NET, VBScript). That cuts out most of the easy languages. Spread your wings! Your solution should take into account null strings and strings whose length is limited only by available storage. ———————————————————————– ** Longest common substring found is: ings — Press any key to exit –PermalinkPosted27-Jan-17 11:20amGraeme_Grant48.3KCommentsppolymorphe27-Jan-17 17:37pmMay be I am tired, but in output, What is first string and what is second ?Graeme_Grant27-Jan-17 17:40pmMy input is in the quotes Yes, the classic … available storage. – thats 5 strings, 2 are nulls. The output LCS after running the code over the input is ingsppolymorphe27-Jan-17 17:47pmOk I see.

Mine [Pascal implementation] was based off the HashSet as a concept as it was based off the F implementation (that was prototyped in a few minutes in C) that in fact uses a HashSet. ;)Graeme_Grant29-Jan-17 21:29pmAlso, Pascal lacks many of the language tools found in the DotNet Framework, so you will also need to implement them too …. things like sorting, manually removing null entries from split strings, HashSets, etc…PIEBALDconsult29-Jan-17 23:55pmWell, if I knew how to access .net from Pascal… :cool:Graeme_Grant30-Jan-17 2:18amyes, modern-day devs are spoiled…Graeme_Grant29-Jan-17 21:35pmA quick hint… while doing the Pascal version, I found that a HashSet is not required.PIEBALDconsult29-Jan-17 23:54pmMy algorithm uses it. If I could be assured of 64 or fewer strings, Id use bitwise operators on an Int64 instead.

Your solution should take into account null strings and strings whose length is limited only by available storage.

If a question is poorly phrased then either ask for clarification, ignore it, or

Graeme_Grant28-Jan-17 15:19pmfair enuf…Graeme_Grant28-Jan-17 18:51pmI found the FreePascal online IDE a couple of hours ago, so as I have never done Pascal before, I gave it a go and you can see what I did below. Oh, how I missed my favorite IDE & the power of the .Net frameworks!PIEBALDconsult28-Jan-17 20:29pmYeah, I loved Pascal back in the day, particularly Turbo, but once I became comfortable with C I stopped using it. I cant even read my Pascal programs from college anymore.Graeme_Grant29-Jan-17 21:24pm[wrong place, so moved]PIEBALDconsult29-Jan-17 20:52pmOn the other hand…

I have a working implementation in C to prove an algorithm I devised.

Please subscribe me to the CodeProject newslettersSubmit your solution!

Yes, the classic LCS problem. Given two or more strings find the longest common substring in each string.

openSystemopenopenSystem.Linqletmsg =Yes, the classic LCS problem. Given two or more strings find the longest common substring in each string.\n\ \n\ One caveat: No C, C, C++, Javascript or any C-based programming language[^], nor any VB-based language (VB, , VBScript). That cuts out most of the easy languages. Spread your wings!\n\ \n\ Your solution should take into account null strings and strings whose length is limited only by available storage.lettests = msg.Split [\n]letGetSubstrings (s :string) = [forc1in0.. s.Length -1doforc2inc1 .. s.Length -1doyield s.[c1..c2] ]letaddRange input (targetSet : ICollection_) = input Seq.iter(funx – targetSet.Add(x))letGetLongestCommonSubstring (strings :string[]) =match(Seq.toList (strings.Where(funx – not(x.Equals()))))with[] -emptyx::xs -letsubStrings =newHashSetstring() addRange (GetSubstrings(x)) subStrings xs List.map(funy -letyy =newHashSetstring() addRange (GetSubstrings(y)) yy tersectWith(yy)) ignore subStrings.OrderByDescending(funs – s.Length).First() [EntryPoint]letmain argv = printfn———————————————————————–\r\nprintfnInput strings (including nulls / blank lines):\r\nprintfn%A\r\nmsg printfn———————————————————————–\r\nprintfn** Longest common substring found is: %A\r\n(GetLongestCommonSubstring(tests)) printfn\r\n– Press any key to exit –; Console.ReadKey() ignore;0

*How to Run Turbo Pascal v7.0 on Windows 7/8^]PIEBALDconsult28-Jan-17 12:59pmIf I wanted it Id have it already. I used to use Turbo Pascal (and Turbo C), but I dont anymore.

how to display the longest palindrome in a string of characters?

System.Collections.Generic.Liststring() ; System.Collections.Generic.DictionaryPIEBALD.Type.Substring,System.Collections.Generic.HashSetint temp =

Subject [ i ].Length ) shortest = Subject [ i ].Length ;

The second one was only after a chat with PIEBALDConsultant… Never done Pascal before so I thought that Id try it.

A couple of around each string would have been clearer to me.

So looking at your Link, R is out but F & PowerShell are okay then ;)ppolymorphe27-Jan-17 15:02pmWhat is supposed to be the result ? Just the length ? The substring ?Graeme_Grant27-Jan-17 15:53pmfind the longest common substring .. my guess is the common substring…Chris Maunder27-Jan-17 16:58pmYes.Jon McKee27-Jan-17 16:43pmPerl is considered C-based but F isnt? Thats a little odd tbh. If anything Perl has less in common with C than F. Also that link includes B which was the precursor to C which I find baffling.Graeme_Grant27-Jan-17 18:07pmId blow the dust off my C64 but basic was not allowed…Jon McKee27-Jan-17 18:40pmHe specified VB. Didnt say anything about just BASIC ;)Graeme_Grant27-Jan-17 18:57pmI planted my flag with F (second time Ive every used it apart from 3 weeks ago – was/is a steep learning curve)… Busy week ahead though, will have to see how it pans out and if I can find the old C64…PIEBALDconsult27-Jan-17 18:42pmVB is not allowed, real BASIC is.

Turbo Pascal (5.5+) has OOP, and HP Pascal probably doesnt — I learned data structures on VAX/DEC/HP Pascal back in the 80s.

Find length of longest substring with all 1s in a binary string with k queries

Leave a Reply