Problem Statement
Note to plugin users: there is an image in this problem statement. Please view the statement in the applet to see the image
Tire rotation is a simple but effective part of vehicle preventive maintenance. Without it, the tires of a car may wear out thousands of miles early. The idea is to have each tire spend part of its life on each wheel of the car. To accomplish this, the tire on each wheel is moved to another wheel according to a pattern. First, we assume the wheel positions are numbered left to right, front to rear. Then we establish a rotation pattern:
From the diagram, we see that for each phase of the rotation cycle, a tire is moved from one wheel position to another, according to the following chart:
starting ending wheel wheel 1 ---------> 3 2 ---------> 4 3 ---------> 2 4 ---------> 1
Therefore, if our four tires are represented by A, B, C, and D, there are four valid phases of the rotation cycle:
Phase: 1 2 3 4 Tires: A B ---> D C ---> B A ---> C D C D A B D C B A ^ | |__________________________|
Write a method will take a
Definition
- Class:
- TireRotation
- Method:
- getCycle
- Parameters:
- String, String
- Returns:
- int
- Method signature:
- int getCycle(String initial, String current)
- (be sure your method is public)
Constraints
- initial will only contain capital letters ('A' - 'Z', inclusive), and will be exactly 4 characters long.
- initial will not have any repeated characters.
- current will be exactly 4 characters long, and will contain the same characters that are in initial.
- current will not have any repeated characters.
Examples
"ABCD"
"ABCD"
Returns: 1
These tires have not been rotated yet.
"ABCD"
"DCAB"
Returns: 2
The initial locations of the tires are: A B C D After one rotation, the locations of the tires are: D C A B
"ABCD"
"CDBA"
Returns: 4
Continuing the rotation, we get the following for phase 3: B A D C And finally, on phase 4: C D B A
"ABCD"
"ABDC"
Returns: -1
Here, the rear two tires were moved incorrectly, and the front two were not moved at all.
"ZAXN"
"XNAZ"
Returns: 4
"EPZQ"
"PEQZ"
Returns: 3
"MHXB"
"MHXB"
Returns: 1
"AXRS"
"RSXA"
Returns: 4
"ELAM"
"AMEL"
Returns: -1
"BVST"
"VBTS"
Returns: 3
"TGEC"
"GTCE"
Returns: 3
"YLTQ"
"QTYL"
Returns: 2
"ONMZ"
"ONMZ"
Returns: 1
"CEOM"
"OMEC"
Returns: 4
"IXJN"
"NJIX"
Returns: 2
"AOFH"
"FHOA"
Returns: 4
"RNLU"
"RNLU"
Returns: 1
"BUMG"
"MBGU"
Returns: -1
"FQTR"
"QFRT"
Returns: 3
"ZATY"
"TYAZ"
Returns: 4
"RZHX"
"RZHX"
Returns: 1
"RNES"
"ERSN"
Returns: -1
"EXKG"
"XEGK"
Returns: 3
"NFHI"
"NFHI"
Returns: 1
"MLHZ"
"LMZH"
Returns: 3
"FJNP"
"NPJF"
Returns: 4
"RMBN"
"NBRM"
Returns: 2
"QXOF"
"XOFQ"
Returns: -1
"QROB"
"BOQR"
Returns: 2
"GQUR"
"RUGQ"
Returns: 2
"ZECM"
"EZMC"
Returns: 3
"GUTS"
"UGST"
Returns: 3
"HUEW"
"UHWE"
Returns: 3
"GEMB"
"EGBM"
Returns: 3
"JRSU"
"JRSU"
Returns: 1
"YVQR"
"RQYV"
Returns: 2
"DYTQ"
"QTDY"
Returns: 2
"LIMP"
"IMPL"
Returns: -1
"HMKR"
"RKHM"
Returns: 2
"GBQZ"
"GQBZ"
Returns: -1
"AOIB"
"OABI"
Returns: 3
"OYXF"
"OYXF"
Returns: 1
"WXIZ"
"XWZI"
Returns: 3
"HYFN"
"YHNF"
Returns: 3
"RCNX"
"XNRC"
Returns: 2