Solet's lookatthefirstmostcommonsolution, whichisthehorizontalmethodwe'regonnatakein a rayhereoffourdifferencewords, we'regoingtohaveAppleapplyeightandat, andthegoalisdefinedthelongestcommonprefix, whichinourcaseweknowisgoingtobe a nowwiththehorizontalmethod, we'regoingtotakethefirstandthesecondwordfromtherape, andwe'regoingtocomparethemletterbylettertoseewheretheyhavesimilaritiesanddifferences.
Sowe'refirstgonnalookatthe A, whichisthefirstletterinbothofthem, andweseethattheybothstartwith a Sowecontinueontothenextletter, whichis P TheyallwearthesameGotothenextdoor, whichis p thenextone, whichis l stillthesame.
Andwefinallygettothelastwordwhichis e andwhy?
Andweseeifthere's a difference.
Soweknowthelongestcommonprefixinthosetwoisgoingtobe a PPL.
Sothenextmonththatwecouldtake a lookatwhichavoidsthatproblemiscalledtheverticalmethod, andthewaytheverticalmethodworksisinsteadofcomparingtwowordsat a time, youcompareallthewordsatonce, butyoucomparethemoneletterat a time.
So, forexample, wetaketheveryfirstcharacterfromallofourdifferentwords, whichinourcaseis a forApple A forapply a for a pin, A Fredandwesay, iseachoneofthesecharactersexactlythesame.
Butonethingtonoteisthattheverticalslicemethodhas a bettertimetosolvetheproblem.
Inthebestcasescenariointhehorizontalmethod, let's justtake a lookat a reallycontrivedexamplewhereyouhaveanarrayof 1000 differentwords, andallofthewordsareexactlythesame.
Thisisgoingtoreturntousinjust a singlestringthatwecanuse.
Thennow, oncewehaveallofourstringsinsideof a loopaswallsovercharactersandLuke, weneedtodothecomparisontomakesurethatourstringsallhavethesamecharacteratthebeginning.