Statistics

Problem Statement for "TireRotation"

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 String initial and a String current, which will both represent the tires on a car. Each character will be a capital letter ('A'-'Z') and will represent a serial number that identifies a tire. initial will be the starting locations of the tires, and current will be the current locations. The position of a character represents the wheel that the tire is on. The characters represent the wheels in the order: 1, 2, 3, 4 (from the diagram above). Using the rotation pattern above, your method should return a 1, 2, 3, or 4 if the tires are in the 1st, 2nd, 3rd, or 4th phase of the rotation cycle. If the tires have been rotated improperly (that is, they are not in any phase), your method should return -1.

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

  1. "ABCD"

    "ABCD"

    Returns: 1

    These tires have not been rotated yet.

  2. "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

  3. "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

  4. "ABCD"

    "ABDC"

    Returns: -1

    Here, the rear two tires were moved incorrectly, and the front two were not moved at all.

  5. "ZAXN"

    "XNAZ"

    Returns: 4

  6. "EPZQ"

    "PEQZ"

    Returns: 3

  7. "MHXB"

    "MHXB"

    Returns: 1

  8. "AXRS"

    "RSXA"

    Returns: 4

  9. "ELAM"

    "AMEL"

    Returns: -1

  10. "BVST"

    "VBTS"

    Returns: 3

  11. "TGEC"

    "GTCE"

    Returns: 3

  12. "YLTQ"

    "QTYL"

    Returns: 2

  13. "ONMZ"

    "ONMZ"

    Returns: 1

  14. "CEOM"

    "OMEC"

    Returns: 4

  15. "IXJN"

    "NJIX"

    Returns: 2

  16. "AOFH"

    "FHOA"

    Returns: 4

  17. "RNLU"

    "RNLU"

    Returns: 1

  18. "BUMG"

    "MBGU"

    Returns: -1

  19. "FQTR"

    "QFRT"

    Returns: 3

  20. "ZATY"

    "TYAZ"

    Returns: 4

  21. "RZHX"

    "RZHX"

    Returns: 1

  22. "RNES"

    "ERSN"

    Returns: -1

  23. "EXKG"

    "XEGK"

    Returns: 3

  24. "NFHI"

    "NFHI"

    Returns: 1

  25. "MLHZ"

    "LMZH"

    Returns: 3

  26. "FJNP"

    "NPJF"

    Returns: 4

  27. "RMBN"

    "NBRM"

    Returns: 2

  28. "QXOF"

    "XOFQ"

    Returns: -1

  29. "QROB"

    "BOQR"

    Returns: 2

  30. "GQUR"

    "RUGQ"

    Returns: 2

  31. "ZECM"

    "EZMC"

    Returns: 3

  32. "GUTS"

    "UGST"

    Returns: 3

  33. "HUEW"

    "UHWE"

    Returns: 3

  34. "GEMB"

    "EGBM"

    Returns: 3

  35. "JRSU"

    "JRSU"

    Returns: 1

  36. "YVQR"

    "RQYV"

    Returns: 2

  37. "DYTQ"

    "QTDY"

    Returns: 2

  38. "LIMP"

    "IMPL"

    Returns: -1

  39. "HMKR"

    "RKHM"

    Returns: 2

  40. "GBQZ"

    "GQBZ"

    Returns: -1

  41. "AOIB"

    "OABI"

    Returns: 3

  42. "OYXF"

    "OYXF"

    Returns: 1

  43. "WXIZ"

    "XWZI"

    Returns: 3

  44. "HYFN"

    "YHNF"

    Returns: 3

  45. "RCNX"

    "XNRC"

    Returns: 2


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: