Problem Statement
The best counterspell is the one with the greatest power. The power of a spell is the product of all its character values modulo 77077, where the value for a character is its position in the alphabet (character 'A' has value 1, character 'B' value 2, character 'C' value 3, and so on). The best possible counterspell for "AA" in the example above is "ZZ", which has a power of 676.
Please note that if the allowed replacements are 'A' to 'B' and 'B' to 'C', the counterspell for "AB" is "BC" not "CC". You cannot change a character that has already been changed, even if it would lead to a more powerful spell.
You will be given a
Definition
- Class:
- Wizarding
- Method:
- counterspell
- Parameters:
- String, String
- Returns:
- String
- Method signature:
- String counterspell(String spell, String rules)
- (be sure your method is public)
Constraints
- spell will contain between 1 and 13 characters, inclusive.
- spell will contain only uppercase letters ('A'-'Z').
- rules will contain exactly 26 characters.
- rules will contain only uppercase letters ('A'-'Z') and dashes ('-').
Examples
"AA"
"Z-------------------------"
Returns: "ZZ"
The example from the problem statement.
"AB"
"ZS------------------------"
Returns: "ZS"
The possible counterspells are "AB", "A", "B", "Z", "S", "ZB", "AS" and "ZS". "ZS" is the most powerful.
"ZZZZ"
"-------------------------Z"
Returns: "ZZZZ"
"ABCDE"
"ZYXXYXZZXYXXZZXZYYXZZZX---"
Returns: "ZXXE"
"ABCDEFG"
"ZSZISJSAJFDAJCHJAABDEFGHJI"
Returns: "ZSZF"
"GKKMG"
"--------------------------"
Returns: "GKKM"
"ABCDEFGHIJKLM"
"ZZZZZZZZZZZZZZZZZZZZZZZZZZ"
Returns: "ZEZZZZZZZZ"
"A"
"Z------------------------A"
Returns: "Z"
"SGJSFGJKF"
"AD-FKNJKAXCKJFGTOJ-AFDASD-"
Returns: "GJJJK"
"ZZZZZZZZZZZZZ"
"ZZZZZZZZZZZZZZZZZZZZZZZZZZ"
Returns: "ZZZZZZZZZZZ"
"ABCDEABCDEABC"
"ACBDESKADSLOEDDDASDBADEDAE"
Returns: "CCDECCECC"
"BAB"
"BC------------------------"
Returns: "CBC"
"A"
"AD-FKNJKAXCKJFGTOJ-AFDASD-"
Returns: "A"
"AAAAAAAAAAAAA"
"BD-FGNJHAXCKJFGTOJ-AFDASD-"
Returns: "BBBBBBBBBBBBB"
"AAAAAAAAAAAAA"
"AD-FKNJKAXCKJFGTOJ-AFDASD-"
Returns: "A"
"CODEANDGLORY"
"--------------------------"
Returns: "NGLORY"
"AVADAKEDAVRA"
"OHTHEREISNOGOODCOUNTERSPEL"
Returns: "OROHOKORUO"
"JAVA"
"NOGOODCANCOMEOUTOFTHISCASE"
Returns: "JNVN"
"SAVECODEOFTEN"
"THISISAGOODADVICETOGIVE---"
Returns: "OTIIOSIOSTI"
"VALARMORGHUL"
"--SONGS-OF-FIRE-AND-ICE---"
Returns: "CLNOSHUL"
"MEDIUMCASE"
"-ADF-ASDFE-ASDFE-ADFADF-ER"
Returns: "MDFMS"
"LARGETESTCASE"
"-THIS-IS-A-LARGE-TESTCASE-"
Returns: "RIEES"
"Z"
"BCDEFGHIJKLMNOPQRSTUVWXYZA"
Returns: "Z"
"ZZZZZZZZZZZZZ"
"ZZZZZZZZZZZZZZZZZZZZZZZZZA"
Returns: "ZZZZZZZZZZZ"
"ABCDEFGHIJKL"
"ZZZZZZZZZZZZZZZZZZZZZZZZZA"
Returns: "ZZEZZZZZZZ"
"B"
"WWWWWWWWWWWWWWWWWWWWWWWWWA"
Returns: "W"
"BBBBBBBBBB"
"WWWWWWWWWWWWWWWWWWWWWWWWWA"
Returns: "BBBWWWWWW"
"GKMKG"
"--------------------------"
Returns: "GKMK"
"ABCDE"
"GKMKG---------------------"
Returns: "GKMKE"
"BDCAEAF"
"-KMKGG--------------------"
Returns: "KKMGF"
"AFKHDSKAS"
"ADJFNJALDJKADIFJAEIADKKDJI"
Returns: "FKLFII"
"A"
"--------------------------"
Returns: "A"
"ABBAABBA"
"REMEMBERWORMSII-----------"
Returns: "BERREER"
"AZAZAZAZAZAZ"
"Z------------------------A"
Returns: "ZZZZZZZZZZZ"
"ABCDEABCDEABC"
"ACBDESKADSLOEDDDASDBADEDAE"
Returns: "CCDECCECC"
"ABCIVO"
"A----A----A----A----A-----"
Returns: "BCIVO"
"AAA"
"--------------------------"
Returns: "A"
"A"
"-------------------------Z"
Returns: "A"
"ZZZZZZZZZZZZZ"
"--------------------------"
Returns: "ZZZZZZZZZZZ"
"AAAAAAZZZZZZZ"
"ZZZZZZZZZZZZZZZZZZZZZZZZZZ"
Returns: "ZZZZZZZZZZZ"