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.
publicstaticStringgridSearch(List<String>G,List<String>P){intcounter=1;for(inti=0;i<G.size();i++){Stringrow=G.get(i);// Finding the occurence of the first pattern row intstartCol=row.indexOf(P.get(0));intendCol=startCol+P.get(0).length()-1;// Finding all occurence of pattern in the same rowwhile(startCol!=-1){counter=1;// Looping over the next pattern rows & next grid rowsfor(intj=1,k=i+1;j<P.size()&&k<G.size();j++,k++){if(G.get(k).substring(startCol,endCol+1).equals(P.get(j))){if(++counter==P.size()){return"YES";}}else{break;}}// UpdatingstartCol=row.indexOf(P.get(0),startCol+1);endCol=startCol+P.get(0).length()-1;}}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 →
Java