Problem Statement
Marge is rather concerned about the hoodlums that her son has been spending time with. Aside from all of the time spent at the arcade, they communicate with each other through encrypted messages. As a concerned parent, Marge wants to discover what the boys are talking about.
Marge knows parts of the boys' code library, given as a
Marge has recovered an encoded message that she would like to decode. Unfortunately, she does not have a full copy of the library, so there may be some SEQUENCEs that do not correspond to any entries in the library; if this happens, you are to replace it with a '?' (Marge is a smart mom, and might be able to figure out what the word is with only partial information). You are to return a
Definition
- Class:
- MorselikeCode
- Method:
- decrypt
- Parameters:
- String[], String
- Returns:
- String
- Method signature:
- String decrypt(String[] library, String message)
- (be sure your method is public)
Constraints
- library will contain between 1 and 50 elements, inclusive.
- Each element of library will contain between 3 and 50 characters, inclusive.
- Each element of library will be formatted as "C SEQUENCE" as described in the problem statement.
- In each element of library, C will be an uppercase letter ('A'-'Z').
- In each element of library, SEQUENCE will consist only of '.' and '-'.
- There will be no duplicate SEQUENCEs in library.
- message will contain between 1 and 50 characters, inclusive.
- Each character of message will be '-', '.', or a space (' ').
- There will be no leading, trailing, or consecutive spaces in message.
Examples
{"O ---", "S ..."}
"... --- ..."
Returns: "SOS"
The example from the problem statement.
{"O ---"}
"... --- ..."
Returns: "?O?"
This time we don't have the S, so we replace the three dashes with question marks.
{"H -", "E .", "L --", "L ..", "O -."}
"- . -- .. -."
Returns: "HELLO"
Note that the clever boys can have multiple ways to encode the same letter.
{"H -.-.-.-.-.-.-.-.-.-.", "I .-.-.-.-.-.-.-.-.-.-", "K -.-.-.-.-.", "E .-.-.-.-.-"}
"-.-.-.-.-.-.-.-.-.-. .-.-.-.-.-.-.-.-.-.-"
Returns: "HI"
A long message doesn't always mean a long word.
{"O ---", "S ...", "B -...", "T -", "R .-.", "E .", "N -.", "X -..-", "D -.." }
"-... --- ... - --- -. .-. . -.. ... --- -..-"
Returns: "BOSTONREDSOX"
{"B -...", "N -.", "H ....", "O --", "Z --..", "G ---", "I ..", "J .---" }
"--- -- -... .- -. .- -. .-"
Returns: "GOB?N?N?"
{"A ."}
"- - - - - - - - - - - - - - - - - - - - - - - - .-"
Returns: "?????????????????????????"
{"A ."}
"- - - - - - - - - - - - - - - - - - - - - - . - .-"
Returns: "??????????????????????A??"
{"O ---", "S ...", "B -...", "T -", "R .-.", "E .", "N -.", "X -..-", "D -.." }
"-"
Returns: "T"
{"O ---", "S ...", "B -...", "T -", "R .-.", "E .", "N -.", "X -..-", "D -.." }
"."
Returns: "E"
{"A .....-----.....-----.....-----.....-----.....---", "B --", "C -"}
"."
Returns: "?"
{"N --.-----.","S ....-.-.","F .--.--","X -.-","V ...-.","J ..-","N .-.--...","M .--.","P -------","M .---..","I .","A --","Z .--.-.","P ....---","P .-.","C --....","U -.","E ..-.---.-","P ..","I .......","Y ---","M .--.-.-.","W -...--.--","B .-.--.-","X -..-.-.","N -","S -.-..-..","K .-","X ..-.","Q ...-.--","L ---.","R ..-----.-","W .---.","P --.","F ---.--..","T ---...-..","E -.-.--..","Z .-..-.--.","J ..-.--","G -....","O .--.--.-.","M ....--"}
"...-.-- ..-. ......."
Returns: "QXI"
{"X ...-.--.-","Y -","Z ..-.----.","U .-..","E ----..-..","I .-.--.","R ..-","H ..-.-...-","O ..------.","X ---.","N --","R ---.-","E -.--..","M -.","C .-","I -..","E .","R ...-.","T -.---","U ....-----","U ....-","Z -.-","R .--","E -.-.-...-","W .-.-.--","Y .---.","B ..","D --.-","X ....","B --..-...","R .-.-.--.","L ...---.","S ..-.-","O -.--.","Z ...--","R ---.-.-.","I --.","G ...--.","S .-.","T ..-.-.","N -.-.--.-","A --..","K ..-.-..--","L ..----","C ..--.--.","B ..---.-..","H .-.---"}
"--..-..."
Returns: "B"
{"R -....","X ...-..-..","K ..--"}
".."
Returns: "?"
{"J ..---.-","R -.-..--..","H -.--....","Y -.","Q ..----","U ..-.--...","W .---","F --...-","H .","U --","B ..----.-","W .--","Z -.-..-","U --.--","F ..-","D ...--","B -.--.-","J -......","L -.-..","C -.--","S .-","C -.....-","T -.--..-","Y ..---","X .-...","Y -.-.","U ..---..","E -..-","F .---.-.-.","O .---.-","M --.","Q .-.--.-","Q ---...--.","Z ---","C .-.--..-.","R ------","M ......--","X -.-.-.","H .-..-.-","J --...","A -..----..","G -----","H --..","D .....--","F --.-.-...","W --.---","S -.---."}
"..---- -.....- .-.--..-. -.-."
Returns: "QCCY"
{"M -","D -..---","M .---.","K ---...-.","Q -.----.--","W --...-.","I .","U -.-..-..","A -.","M .-","J -.--..","T ..----.-.","J --.-","G .....-","G .-.","T ------.-","L .-..----.","U .-....","D ----.--.-","Y ---","G .--......","H -..-","J .--","Q ...--.","C -...","R .--.-","I ...","I -----.","Y ..-","R -.-.","U .---...-","B .---.-","G .-.----.","Q .-...","F --.-.-..","K --..-..-","O ..--.--","G ..--..","N ..","X .---"}
"... ...--. ----.... --...-. .- .....- .---."
Returns: "IQ?WMGM"
{"N --","V ....-","X ---..---","S --..","B ..-","V --.-.","J .--","L -","P -.-..-","B -.","M ..-....","V ...","G .","R ..-.","W -.-..-.--","Q .-","U --.","M -.-.."}
"..-..--.- ....- ....- -- -.-.."
Returns: "?VVNM"
{"N .-.-","U ..-.-","D .--.--.","X ..-.","L .","L ...--","O .-.---..","M -..-.-.","N ....","D -..--","E -...","I ---.-.","M ---.----","N .---.","G .--..-.","Q -.-..--.","Q -.----","H ---","N ...-...","X .-...","Y ..---","D .--.-","J -.--.-","H --...-","C .-","I .-.","C ..-","X ..."}
"..- .-.---.. -.--.- -.. -.--.- -..-.-. -- -..--"
Returns: "COJ?JM?D"
{"F --","R ..-","I ..---.-","C .--.-..","M -...--.","G ...-","I .","U -..-.--","H -.","X -...","K .-.","A ...-.-..","U -..-","D --...---","L .-..","X ...-...--","Y ...---","X .-...-..-","O -...-","A -.-","Y .-.---","D .....-..-","N .---.-.","A ..","G .-..-.-.","O ...-.--","W -.-..-","B -.--.-","U -","Y ..-.","H --...","N .-.--","Z --.-.","T .-...","D -.-...--","J ---.-","O .----.","A ....--","U --..--..","B ..-..","L -..----","Z -..-.","Z --.-..--","N -.-..","X -..","D --.----."}
".-...-..- .. -..---- -..-."
Returns: "XALZ"
{"T ...-.-.","L .-.","K .-.--."}
".-.--. .-. .-.--. .-.--. ...-.-. .-. ...-.-."
Returns: "KLKKTLT"
{"N -","U .--.-.","R --..-","P .--.-","H .---.-.","X ----..-","S ...--","A .--.","Y .","B ..-","D ..-..-.","W -..----.","G .-.","T -.-.-.","H ...","O ..","U .--.--.-","O ..-.--.","B .-","M ...-.-.-","V .--...--","R -.-","X ....-..--","C .-.--","G ..---.-.","X .--","P -..","X ----","E -----.","C --.-..--","C -.---.---","L ..--","K -.---.--","V .-..--","P --.-.-...","T .-.-","N .---..-.","A -.-.-.-.-","X ..-..--","C ...-..-","E -.","V ..--..-","Z ...-","W --."}
".-.--"
Returns: "C"
{"L .","Y ..-..--","E -.-..","M -"}
". ..-..--"
Returns: "LY"
{"H ..-..-.","I --.","U .-","L -.-","X --.-.","U ...-","A ..----.","F ---","Q -.","C -....-","B .-...-..-","D ......-..","T -...--..","B --...-.","O ..","Y -.--.--","X .----...","F .---","T .-----.-.","I ...--..","M --...","D .-.-...","S ..-...-."}
"......-.. ---..-.-"
Returns: "D?"
{"P -.----.-","V --","I -.....","I .-.-.","Q -.-...-","I ..------","H -","I ..","C ...","Z ....-.-","T --."}
"....- ..- .....--- -. ... --. .-.-. .. ..---.---."
Returns: "????CTII?"
{"Z .","I --....-","H -......","W -.-..-","I -.-----.","R -....-.","B .-.","R -.-","P ---..-","O --..-","W .--.-.","O --..","C ..-.--","X .-...--","K --.-.-.-.","V .----..-","M -...-...","R ..---.","E .........","J -----..-","I -.--.-..","R ..","D ..-"}
"--.-.-.-. -...-... .-.....--"
Returns: "KM?"
{"O ..-..","I .","V ..-","Y ---","C -..-","V -.-","Q ..--","S --.","H -.--..-","I -.-.","R .-.-..---","M -.-...-","J -","T -..--.","X -..-.","W .----...-","Y --.--","W .--..--.","C .....-","I -..","Q .------.-","M -...-.","F -....","H .-.","N --"}
".----...- -.- --.--. -.-.. -.... -..- --"
Returns: "WV??FCN"
{"M -.....","O ..---","V -.-..","H ---.---..","S .--..","E -","Y .---","A -..-.--","X .--..-..","X ..--.","Y -.-.-","S -.-.-....","T .","B .-..-..-"}
".--.."
Returns: "S"
{"N ---.-","I --.","L ..-..--.","W -","W .-----.","S .-.---.-.","X -.-.-.-.","W ---.-..--","S .-.----.","E .."}
"-.... -"
Returns: "?W"
{"R .-..","E .....-..","O --.-","X --.","R .-.---","V -..---","D ..-.-","W --.-..---","F -..-.-.","Y ---.----","H ---","Y -.--..--","I ..-.-..-","H ..-.---..","T -....---.","P -..-..-","J .","C ..-","E .-.","Z --.--","K -","B .-..-.--","O .---.--.","U ..","Z .----.-.","T .---.---","Q ----","C ---.","Z ...-.....","X .-..-..--","Z -.","V ...------","B -----...","W ...--....","R ..--","V -..-.","L ....-..","V .-","E -.."}
"--.-- -..-..- .---.--- -....---. .-..--...-"
Returns: "ZPTT?"
{"Z --","I .","U --.","E -..---.-","A .-","S .-.---","J ..-","G ---"}
"-.-..--..- ...-...- .-.-- ---- ---.-..- ..--....."
Returns: "??????"
{"M --.","K -.---","S -.","V ..--","D .-.","B ...-","A ..-"}
"-.---"
Returns: "K"
{"B .-.","P ---.-.--.","Y .","A -.-.---","V ---","P .-.....-.","B --","C ....-","N -.------.","O .-","Z -","J --.-.","N -..---","I ....","Z ---.","N .-..-","T -.-----","O ----.--","Y ...--.","D ----","Z -..-.-.","R ---.---.","B --.--.","Z -..----","A --..","D ..-...--","P ..-.--.-","X .--.-","R ..-.-..","L .---","A ..---","A .-...","W .-.---","K -.-","S -.","T -....-.--","Z .-.--","Y ---..","P -...-.-","Z -..","A .-....-","Q .-..----."}
"-.------. .--.- - -. ...--. ..-...-- ---- ..---"
Returns: "NXZSYDDA"
{"C -.","F --..-.","O -","X .-","H ........","C -..--.--","B ..-.....","L ...","F .-.","N -..-..---","V .--","I -.---","E -...--.","M --","O .--------","Z -.-.-.--","Y .---..","Z ---..","Q --.---..-","N -..","Z .----..--","Q .-..-----","I .......","M .----.","Z -----","L ..-","L .---.-...","E ...---","L -...-.-","O --..---..","D --....-","X .--.-.","D ---.","A -----.-","P .-.-..-..","A -.-","W -...","M -.--"}
"--.---. --.---..- ----..... - .--"
Returns: "?Q?OV"
{"H .-","Y ..-","O .-..-.-","I .-...","R --","V --.-.-","F ---.","K -.---.--","P .-...-..","N .","W -.-","E ..-..-","L ..","N -.---","J ..---.-.","E ..."}
"--.-.- ..-"
Returns: "VY"
{"M ---..","B .","A ..-.-.","S -.-","O -.....","C -----.-","D ---.....-","W -.------.","U ....","I ..-.---.","I ..-","A ----","Q -....-","B -.-.","X ....-.-"}
"-.-. .-..-..- -....."
Returns: "B?O"
{"K .....-","J ---.-","Y .--.-.-","X ..-.....","W -.--.-.--","H .","U .-.---","P -...-","O .-..-..-.","O .........","D ----..-.","K .-.-","L ---....-"}
".-.- -- ----.-. .-..-..-. ----..-. -.--.-.--"
Returns: "K??ODW"
{"N -....-.","U ---..-","R .--.","T -.-.-..","X .--.--...","P -","E .-","I --.--.","H ..","F .---","E ..--","I .---..","E --.","S ..-..","F .--","H ---","I -..","I .-.","U -.--","F --.--.---","T .-..--","I .-..-","K ..-..-","N .-.-","O .....-..."}
"..--- -.-- .....-... .-.-. -....-. .---.. --. .--."
Returns: "?UO?NIER"
{"T ...---.-.","F .-","K ..--.-","L -","L --.---.","Z -.","O .....-","K ......","Y .---","N ..-","O .--.-----","K .","U .--.-..","F .--.","V ..","C -..-..-","U -.--..-..","D .----.","V --.-..--","G --..-.","A .--","J -.-.","O .-.-.","Y ...","Q .--...","C ...---"}
".--.-.. -. .... -.... .--... -. .----. .--. -"
Returns: "UZ??QZDFL"
{"S .-.-..--.","B .","Q -","Q .--.-.","Q --....-","U -..--.-","M --","S -.......-","Y ..---","Y -...-..","D .-.","N -----.-","G ..","M ...-...","B -.---.-","D ---.","M ..-..","V --.---."}
"..-.--- --.-. -...-.. -- -. .-. ..--- --.--.- ."
Returns: "??YM?DY?B"
{"A --", "B -.", "N ...-", "I --..", "F -.-.-."}
"-. -- -.-.-. -.-.-. --- --.. ...- .-..--."
Returns: "BAFF?IN?"
{"O ---", "S ..." }
"... --- ..."
Returns: "SOS"
{"B -...", "N -.", "H ....", "O --", "Z --..", "G ---", "I ..", "J .---" }
"--- -- -... .- -. .- -. .-"
Returns: "GOB?N?N?"
{"A --", "B -.", "N ...-", "I --..", "F -.-.-." }
"-. -- -.-.-. -.-.-. --- --.. ...- .-..--."
Returns: "BAFF?IN?"
{"O ---" }
"... --- ...-"
Returns: "?O?"
{"L --", "E .", "H -", "L ..", "O -." }
"- . -- .. -."
Returns: "HELLO"
{"O ---", "S ...", "B -...", "T -", "R .-.", "E .", "N -.", "X -..-", "D -.." }
"-... --- ... - --- -. .-. . -.. ... --- -..-"
Returns: "BOSTONREDSOX"
{"A .", "A -" }
". -"
Returns: "AA"
{"O ---" }
"--- ... ..."
Returns: "O??"