Problem Statement
We will restrict our word to uppercase letters. In the font we are using,
- H I N O S X Z look unchanged after rotation
- M W look like each other after rotation
Some examples of ambigrams are "MOW", "XXXXXXXXXXX", "XMIWX", "HXHXHXHXHXH".
We want to make minimal changes to the given word to produce an ambigram. We will measure the cost of changing by distance within the alphabet, so changing an 'F' to an 'H' would incur a cost of 2, and changing an 'I' to an 'F' would cost 3. If we keep changing a letter until we run off either end of the alphabet, the letter disappears. (When a letter disappears, letters around it slide together.) So we can make a C disappear at a cost of 3, and can make a 'T' disappear at a cost of 7.
Create a class Ambigram that contains a method ambiword that is given a
Definition
- Class:
- Ambigram
- Method:
- ambiword
- Parameters:
- String
- Returns:
- String
- Method signature:
- String ambiword(String word)
- (be sure your method is public)
Constraints
- word will contain between 1 and 50 characters inclusive.
- Each character in word will be an uppercase letter 'A'-'Z'.
Examples
"BXC"
Returns: "X"
We can remove the 'B' at a cost of 2 and the 'C' at a cost of 3. Total cost = 5. Note that "BXB" is NOT an ambigram.
"XIXHZMOAOSHXIX"
Returns: "XIXHMOOWHXIX"
This can be done at a cost of 6 (make the 'A' disappear, make the 'Z' disappear, change 'S' to 'W' at a cost of 4).
"C"
Returns: "H"
We could make the 'C' disappear at a cost of 3, but the result would be the empty String, which is not an ambigram. At a cost of 5 we can change the 'C' into an 'H'.
"AMWZ"
Returns: "MW"
Note that we cannot change the 'A' to 'Z' cheaply by treating the alphabet as a circle. "ZMWZ" would be a longer ambigram than "MW" but it would cost 25.
"ABCDEFGHIJKLMNOP"
Returns: "HHHHIIIIHHHH"
"MIW"
Returns: "MIW"
"MWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMW"
Returns: "MWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMW"
"MWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWM"
Returns: "NMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWN"
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
Returns: "H"
"DDDDDDDDDD"
Returns: "HHHHHHHHHH"
"FDKLKJSDKJSAKJDASDLKDLKHALKASKL"
Returns: "HHINHIWHIISIISIIHMIHNIHH"
"GLSMSMWMSIDHSFLLSMDMMWMWLSFKDFH"
Returns: "HISNWNWNSIHHSHHISNMNMNSIH"
"HEREISASTRINGOFMODESTLENGTH"
Returns: "HHNHISSONHNHNHNOSSIHNHH"
"WHOKNOWSWHATTHISMIGHTBE"
Returns: "HMHHIWSWHSSHMSMIHHWH"
"LLLLLSTUVW"
Returns: "MMMMMWWWWW"
"ZZZZZZZZZZZZ"
Returns: "ZZZZZZZZZZZZ"
"ZZYZZZZZZZZZZ"
Returns: "ZZZZZZZZZZZZZ"
"Y"
Returns: "X"
"ZZZZZZYZZZZZZ"
Returns: "ZZZZZZXZZZZZZ"
"DDDDDDDDDDD"
Returns: "HHHHHHHHHHH"