Well, onebruteforcetechniqueisListen, you'regiventhisindustry, Theo, anditssizeis K andyouhaveanarrayofsize.
Andthenwhatyouneedtodoisyouilliteratethrougheachelementof K, likeyourfirsttake a lookattheyoumashupwithallofthefirstmattersinthisarrayofsize.
Andyou, underourotherwords, thatdon't startwiththeandthenyougotothenextletterin K andtake a lookatOh, andyoufilterthrougheverythinghereandessentiallythiswillgetyou a solution, buttherunningtimeisgoingtobebig o of K timesandandthat's okay, But I'm wonderingifwecanevenmakethisfasterAndthat's wherewecanuse a tried a disrupter t r i E.
SothewaythetragedyDestructorworksisactuallylooksverysimilarto a standardtree.
Youhave a writtenup, andthenyouhavelettersateachothernote.
Soyouhave B, C O G.
Andanytimeyouhaveactualwork, yourmarketlikehit, thisis a workyoumayhave a r k.
Hereyouhave t o r sofastspellsouttheworkdoorandthentheoh, the g e forDutch.
Andso, essentially, onceyouhavethistried a thestructurebuiltout, you'llbeabletoidratethroughit.
Likeifyou'relookingfor d o youfirsttraversetoe, tryoverto D o ontothisnoteAndthenfromhereyourun a deathfirstsearchlookingforallofthisupknowsthatform a completework, andthosearen't thealthoughcompletionresultstoreturn.
Thenumberofwordsaregoingtobe a maximumofOh, right, becausewecouldpotentiallyreturneverysingleresult.
AndViggoistheworstcasetimecomplexity.
Sothisisgoingtobeyourplanetcomplexity, andthat's betterthanwhatwehadpreviously, K Timessaid.
Spacecomplexityisgoingtobe.
Thesizeofthistrayisprettymuch a weapon, whichisthetotalsizeofftheinputs.
Let's take a lookatwhatthecoatlookslike.
Soforthetry, I'llstartoffbycreating a knowthattheydestructor.
Then I willcreate a solutionclassand I'm goingtocreatethebuildfunctionhereandthisisgoingtobuildthetry.
Thenwe'regoingtohaveanothermethodherecalled, althoughcomplete, which, given a prefix, itwillreturn a setofwordsandletmecomeupwiththedriverfunctionhere.
Youneedtogetthecaseelements, soyouneedtorunthepopoperation K times, andeachoperationisgoingtotakelongofendtimebecauseitneedstotraversetheheightofthetree.
Soifyouthinkaboutthat, theheapisactuallydoingsomeadditionalworkbecauseinadditiontogettingtheKateelement, you'realsogettingthe K minusoneelementbecameonthesecondelementthatcameonthisthirdelement.
Andessentially, you'vesortedhalfoftheory.
Perhapsbythetimeyoufoundthatcaseelement, sowecanactuallyavoidsorting a lotofthisarraybyusing a specialtechniqueknownaspartitioning.
Sohere's howpartitionworks.
Youpick a randomnumbertraditionally, you'rejustpickthelastoneandthenyou'regoingtopartitionaroundit.
Sothenwe'lltake a look a c a andthen T s d o g thesDon't matcheither.
C a.
T asTheo G wherewefound a matchforcat.
Butwedidnotfind a matchfor s stock.
C a t s t o g.
Wefound a matchforthatandthenwecancontinueonandcheckoutTheothercombinationswereessentiallyjustpartitioningthisworddownandkeepinmindthisisactuallygoingtobe a recursiveprocess.
Somaybewecanjustmake a matrixwherethestartingindexare.
Thecolumnsandthentheendingindexaretherose.
Andsoforeachentryinthematrixrepresents a wordlikethis, a c, it's a C a c a T c A t s a 80 80 s, t.
T s, Andthenthisis s and I havetomakeyourex.
Well, Hume, there's nothingreallythere.
Solookatitthisway.
Thetotalnumberofcombinationsisactually m scoredoverto, whichmeansthatouroptimizationhasbroughtourrunningtimetoendtimes M squared, whichseemsprettygoodtome.
I mean, though, ifyouwereabletofindsomethingfasterthanthis, Butotherthanthat, thisseemslike a prettydecentsolution.
Letmeshowyouwhatthecoatlookslike.
So I'llstopbycreating a classandilldefinedthefunction.
Andthenwhen I runthisprogram, I'm expectingtheoutputcatstuck.
Sothefirstthingwe'regonnadoisturnthesewordsinto a setsuchthatwegettheconstanttime.
Lookupwhenwe'rechecking, everywordexistsinhereandthenhydratethrougheachwordandreturnitifitis a validcontamination.
So I createdtheprivatemethodcanformandthen I wouldratethrougheachletterinthewordandthen I'llsplittheworldinto a prefixandthesuffixandcheckwhetherbothhalvescanbeconstructed.