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.
publicstaticbooleansuccess=false;publicstaticbooleanisOk(Stringtmp,Stringword){for(inti=0;i<tmp.length();i++){charc1=tmp.charAt(i);charc2=word.charAt(i);if(c1!='-'&&c1!=c2){returnfalse;}}returntrue;}publicstaticvoidfillIn(intstarti,intstartj,Stringword,char[][]board,charsymbol){if(symbol=='-'){for(intj=startj;j<startj+word.length();j++){board[starti][j]=word.charAt(j-startj);}}elseif(symbol=='|'){for(inti=starti;i<starti+word.length();i++){board[i][startj]=word.charAt(i-starti);}}}publicstaticvoidfill(intstarti,intstartj,char[][]board,boolean[]visited,String[]wordList,intturn){if(turn==0){success=true;return;}inti=0;intj=0;booleanfind=false;for(i=starti;i<10;i++){for(j=0;j<10;j++){if(j+1<10&&board[i][j]!='+'&&board[i][j+1]=='-'){find=true;break;}if(i+1<10&&board[i][j]!='+'&&board[i+1][j]=='-'){find=true;break;}}if(find)break;}if(j+1<10&&board[i][j]!='+'&&board[i][j+1]=='-'){Stringtmp="";for(intx=j;(x<10&&board[i][x]!='+');x++){tmp+=board[i][x];}for(intx=0;x<wordList.length;x++){if((visited[x]==false)&&(wordList[x].length()==tmp.length())&&isOk(tmp,wordList[x])){visited[x]=true;fillIn(i,j,wordList[x],board,'-');fill(i,j+1,board,visited,wordList,turn-1);if(success==true)return;fillIn(i,j,tmp,board,'-');visited[x]=false;}}}elseif(i+1<10&&board[i][j]!='+'&&board[i+1][j]=='-'){Stringtmp="";for(intx=i;(x<10&&board[x][j]!='+');x++){tmp+=board[x][j];}for(intx=0;x<wordList.length;x++){if((visited[x]==false)&&(wordList[x].length()==tmp.length())&&isOk(tmp,wordList[x])){visited[x]=true;fillIn(i,j,wordList[x],board,'|');fill(i,j+1,board,visited,wordList,turn-1);if(success==true)return;fillIn(i,j,tmp,board,'|');visited[x]=false;}}}}// tinhs lai cai turnpublicstaticList<String>crosswordPuzzle(List<String>crossword,Stringwords){// Write your code hereList<String>res=newArrayList<>();String[]wordList=words.split(";");char[][]board=newchar[10][10];boolean[]visited=newboolean[wordList.length];for(inti=0;i<10;i++){for(intj=0;j<10;j++){board[i][j]=crossword.get(i).charAt(j);}}fill(0,0,board,visited,wordList,wordList.length);for(inti=0;i<10;i++){Stringtmp="";for(intj=0;j<10;j++){tmp+=board[i][j];}res.add(tmp);}returnres;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Crossword Puzzle
You are viewing a single comment's thread. Return to all comments →
What a long problem! But it's happy when finish