Problem Statement
This problem is about the popular word-guessing game Wordle. Knowledge of this game is not necessary to solve the problem.
There are two
When the solver makes a guess, the game announces how good the guess was. This is done using colors. The letters of the guess that are in correct places are highlighted in green and the letters that are in the secret word but at some other index are highlighted in yellow.
Formally, the coloring works as follows:
A pair of indices (i, j) is called valid if guess[i] = hidden[j]. When coloring the letters of guess we will look for valid pairs of indices. Among these pairs each index can only be used at most once: there cannot be two pairs (i, j1) and (i, j2) with j1 != j2, and there cannot be two pairs (i1, j) and (i2, j) with i1 != i2.
We will create these pairs in two passes.
- In the first pass we create all valid pairs of the form (i, i). These correspond to the green letters.
- In the second pass we go through all possible i from 0 to 4, in this order. For each i, if there is a j such that (i, j) is a valid pair disjoint from all the ones we already have, we take the smallest such j and we add the pair (i, j) to our collection. For each pair (i, j) added in this step the letter at position i in guess will be yellow.
Return a
Definition
- Class:
- WordleColors
- Method:
- color
- Parameters:
- String, String
- Returns:
- String
- Method signature:
- String color(String hidden, String guess)
- (be sure your method is public)
Constraints
- hidden will have exactly 5 characters.
- Each character in hidden will be an uppercase English letter ('A'-'Z').
- guess will have exactly 5 characters.
- Each character in guess will be an uppercase English letter ('A'-'Z').
Examples
"BLOCK"
"BRICK"
Returns: "g--gg"
The letters 'B', 'C', 'K' all appear in their correct positions and so they are green. The letters 'R' and 'I' of the guess don't appear in the hidden word and so they don't get a color.
"BLOCK"
"BLOBS"
Returns: "ggg--"
The letter guess[3] = 'B' does not get any color. When coloring the letters green for exact matches we already created the pair (0, 0), i.e., we paired guess[0] = 'B' with the corresponding hidden[0] = 'B'. There is no other 'B' in hidden to pair with guess[3].
"BLURB"
"BOBBY"
Returns: "g-y--"
We first find the pair (0, 0) of 'B's in the right position that makes guess[0] green. Then we find the pair (2, 4) of 'B's that are present but not in the same position. The third 'B' in "BOBBY" has no pair, as "BLURB" only contains two 'B's.
"ABBOT"
"BOBBY"
Returns: "yyg--"
"CANAL"
"AAAAA"
Returns: "-g-g-"
"GREEN"
"BLACK"
Returns: "-----"
"RATES"
"STARE"
Returns: "yyyyy"
"LLLLL"
"LLLLL"
Returns: "ggggg"
"DDDDD"
"DDDDD"
Returns: "ggggg"
"HHHHH"
"HHHHH"
Returns: "ggggg"
"SSLLS"
"LSLSS"
Returns: "yggyg"
"VJJVJ"
"JJJVV"
Returns: "ygggy"
"UUUGU"
"GGGGU"
Returns: "---gg"
"QQQTQ"
"QQTTT"
Returns: "gg-g-"
"LLNLL"
"LLNNL"
Returns: "ggg-g"
"OOIOI"
"OIOOO"
Returns: "gyyg-"
"XQXXQ"
"QQQXQ"
Returns: "-g-gg"
"THTTT"
"HTHTT"
Returns: "yy-gg"
"RSRRS"
"SRRRR"
Returns: "yygg-"
"SYYYS"
"SYSYS"
Returns: "gg-gg"
"ZWWZZ"
"WZWWZ"
Returns: "yyg-g"
"OOIOI"
"IIOOI"
Returns: "y-ygg"
"XXXRR"
"RXXXX"
Returns: "yggy-"
"CCDCD"
"CCCDC"
Returns: "ggyy-"
"OOOOG"
"GOGGO"
Returns: "yg--y"
"OOOQO"
"QQOOO"
Returns: "y-gyg"
"NNHNN"
"HNHHH"
Returns: "-gg--"
"CCTTC"
"TCTTT"
Returns: "-ggg-"
"MRRRR"
"MRMMR"
Returns: "gg--g"
"RRNNN"
"RNNNR"
Returns: "gyggy"
"KKMMK"
"MMKMK"
Returns: "y-ygg"
"TTTMM"
"TTMTM"
Returns: "ggyyg"
"WWHWH"
"WHWHH"
Returns: "gyy-g"
"DDDDD"
"DDDDS"
Returns: "gggg-"
"DDLDL"
"DLLLL"
Returns: "g-g-g"
"KKKKK"
"KKKCC"
Returns: "ggg--"
"BKBKK"
"BBKBK"
Returns: "gyy-g"
"NNVVV"
"VNNVV"
Returns: "ygygg"
"YYJYY"
"JJYYJ"
Returns: "y-yg-"
"LELEE"
"ELEEE"
Returns: "yy-gg"
"YIIII"
"PYPPY"
Returns: "-y---"
"MMAGM"
"AAAMM"
Returns: "--gyg"
"CCCCP"
"PCPCC"
Returns: "yg-gy"
"SATAS"
"STSST"
Returns: "gyy--"
"SSUSV"
"VVSVU"
Returns: "y-y-y"
"QQTTQ"
"GGQQG"
Returns: "--yy-"
"UMVVM"
"UUMVU"
Returns: "g-yg-"
"NONXO"
"OOXNO"
Returns: "-gyyg"
"FFJJJ"
"FFJFF"
Returns: "ggg--"
"WNNAA"
"NNNWN"
Returns: "-ggy-"
"UUOOO"
"UOUUO"
Returns: "gyy-g"
"CACAE"
"AAEAA"
Returns: "-gyg-"
"TTPPP"
"PPTTT"
Returns: "yyyy-"
"EJEEJ"
"EEEJT"
Returns: "gygy-"
"ZZNZZ"
"ZNZZZ"
Returns: "gyygg"
"BMTMM"
"TBTBT"
Returns: "-yg--"
"UUULL"
"KKLUL"
Returns: "--yyg"
"ZPNPP"
"PPZNN"
Returns: "ygyy-"
"VFTVF"
"TTFVT"
Returns: "y-yg-"
"LDLDD"
"DEDDD"
Returns: "y--gg"
"HZUGU"
"GGUHU"
Returns: "y-gyg"
"CKKRI"
"CRRCI"
Returns: "gy--g"
"RRRLH"
"RURLH"
Returns: "g-ggg"
"SKSFK"
"SFFII"
Returns: "gy---"
"MCUAM"
"UMCMM"
Returns: "yyy-g"
"NNCCN"
"JRCCN"
Returns: "--ggg"
"PSYYW"
"YSSYW"
Returns: "yg-gg"
"PJQPQ"
"QQQHH"
Returns: "y-g--"
"WQWWQ"
"WWQQI"
Returns: "gyyy-"
"BOCBO"
"OOMBC"
Returns: "yg-gy"
"IMKKK"
"MKMTK"
Returns: "yy--g"
"WPWWA"
"SAAPP"
Returns: "-y-y-"
"MMEOO"
"EEOOM"
Returns: "y-ygy"
"YYHZY"
"ZYYFH"
Returns: "ygy-y"
"HYYZH"
"IYIHI"
Returns: "-g-y-"
"FFLFL"
"FLQLQ"
Returns: "gy-y-"
"COFIF"
"FFFOO"
Returns: "y-gy-"
"VZXVO"
"XZOXO"
Returns: "yg--g"
"UHHHL"
"HAUAA"
Returns: "y-y--"
"CUHJH"
"UJJHU"
Returns: "yy-y-"
"XVVJV"
"VJBJJ"
Returns: "y--g-"
"RVQHR"
"HHNRR"
Returns: "y--yg"
"NCXEC"
"XHEXN"
Returns: "y-y-y"
"IVBBC"
"HBBVB"
Returns: "-ygy-"
"IPHBG"
"HPBPH"
Returns: "ygy--"
"EIYYK"
"EKKKI"
Returns: "gy--y"
"YQQOJ"
"JQJEE"
Returns: "yg---"
"AJWWA"
"AKIKI"
Returns: "g----"
"RHRHV"
"PRPRR"
Returns: "-y-y-"
"LFFGN"
"LLGLN"
Returns: "g-y-g"
"HAOAY"
"XCMPG"
Returns: "-----"
"XQXQD"
"AHDVG"
Returns: "--y--"
"BDVAV"
"QPPGD"
Returns: "----y"
"HUUFY"
"EXEHU"
Returns: "---yy"
"FOKVA"
"IJEXS"
Returns: "-----"
"ERVIR"
"NIINI"
Returns: "-y---"
"FDCMR"
"MMGAM"
Returns: "y----"
"NZZRA"
"RZRCF"
Returns: "yg---"
"RJKZL"
"WIGZN"
Returns: "---g-"
"EGTKK"
"OTOIE"
Returns: "-y--y"
"WKWLO"
"EFHYS"
Returns: "-----"
"SOSOD"
"ZXVYF"
Returns: "-----"
"RABGJ"
"JJAZN"
Returns: "y-y--"
"FVUCC"
"PFULC"
Returns: "-yg-g"
"GGLDM"
"MSROV"
Returns: "y----"
"AOZVQ"
"AEQZJ"
Returns: "g-yy-"
"JFOJO"
"FGAJS"
Returns: "y--g-"
"HINIE"
"JNFZK"
Returns: "-y---"
"GMMPG"
"KHWHH"
Returns: "-----"
"YVAQS"
"ASIGY"
Returns: "yy--y"
"ZUEXA"
"QESRN"
Returns: "-y---"
"WROBX"
"WXWBB"
Returns: "gy-g-"
"ZVMFR"
"SGZYY"
Returns: "--y--"
"WWTJA"
"FIFMP"
Returns: "-----"
"ONHBT"
"BHYWB"
Returns: "yy---"
"OATJP"
"SPQPZ"
Returns: "-y---"
"JTLGK"
"JPGPX"
Returns: "g-y--"
"NWAWA"
"NPPAA"
Returns: "g--yg"
"RCHQH"
"FZLDZ"
Returns: "-----"
"KUMUF"
"YUYKY"
Returns: "-g-y-"
"BBAAC"
"AABBC"
Returns: "yyyyg"
"BAABB"
"BBBXX"
Returns: "gyy--"
"BBBAA"
"AAACC"
Returns: "yy---"
"XAXCD"
"YXAAX"
Returns: "-yy-y"