We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
funcgridSearch(G:[String],P:[String])->String{// Write your code hereguardG.count>=P.countelse{return"NO"}guardG!=Pelse{return"YES"}funcisEq(c1:[Character],start:Int,end:Int,c2:[Character])->Bool{ifend>=c1.count{returnfalse}foriinstart...end{ifc1[i]!=c2[i-start]{returnfalse}}returntrue}letcG=G.map{[Character]($0)}letcP=P.map{[Character]($0)}letfirstP=cP[0]letpCountForIndex=firstP.count-1funccheck(index:Int,indexR:Int,endR:Int)->Bool{guard(cG.count-index)>=cP.countelse{returnfalse}varindexG=indexvarindexP=0whileindexG<cG.count&&indexP<cP.count{if!isEq(c1:cG[indexG],start:indexR,end:endR,c2:cP[indexP]){returnfalse}indexG+=1indexP+=1}returntrue}for(index,row)incG.enumerated(){for(indexR,charR)inrow.enumerated(){ifcharR==firstP[0]{ifcheck(index:index,indexR:indexR,endR:pCountForIndex+indexR){return"YES"}}}}return"NO"}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Grid Search
You are viewing a single comment's thread. Return to all comments →
Swift & charater array