Statistics

Problem Statement for "Defragment"

Problem Statement

When new files are written to a hard disk, oftentimes they are broken up into fragments and stored in different physical locations on the drive. "Defragmentation" is the process of reordering these fragments such that they are all adjacent on the hard disk. Not only does this increase performance of the computer, but it helps prevent new files from being fragmented in the future.

Given a String FAT, representing the hard disk's file allocation table, return a int indicating the number of clusters which will be moved during the defragmentation process. A typical file allocation table might look like the following:

"C.RC.C.CC..C....RCC.."

A 'C' represents a cluster on the disk, an 'R' represents a read-only cluster (one that cannot be moved), and a period ('.') represents a position on the disk which does not correspond to a file. The defragmentation algorithm reads the FAT from right to left, looking for a cluster to move. Once it finds one, it starts searching from left to right (starting at position 0) for an empty space. In the above example, the rightmost 'C' is at position 18 in the string, and the leftmost period is at position 1. The defragmenter then moves the cluster to the empty space, resulting in the new FAT below:

"CCRC.C.CC..C....RC..."

Now, the rightmost cluster is at position 17, and the leftmost empty space is at position 4. After moving this cluster, the FAT is arranged as follows:

"CCRCCC.CC..C....R...."

The next cluster is at position 16, but it is read-only and cannot be moved. Therefore, the defragmenter continues past it to the cluster at position 11, and moves it to the empty space at position 6.

"CCRCCCCCC.......R...."

When the defragmenter now tries to move the cluster at position 8, it discovers that there is no empty space to the left of 8 in which to place it. This means that defragmentation is complete. Three clusters were moved, so for this example your method would return 3.

Definition

Class:
Defragment
Method:
clustersMoved
Parameters:
String
Returns:
int
Method signature:
int clustersMoved(String FAT)
(be sure your method is public)

Constraints

  • FAT will contain between 1 and 50 characters, inclusive.
  • FAT will contain only periods ('.') and the characters 'C' and 'R'.

Examples

  1. "C.RC.C.CC..C....RCC.."

    Returns: 3

    This is the example from above.

  2. "CCRCCCCCC.......R...."

    Returns: 0

    At this stage, the hard disk is already defragmented as much as possible. There are no clusters that need to be moved.

  3. "..........CCCCCC"

    Returns: 6

    Each of the six clusters on the right will need to be moved to the left.

  4. ".RR...RR.RR....RR....R......R.R.RRRR.RR...RRR....."

    Returns: 0

    The disk is highly fragmented, but each cluster is marked as read-only, so no move operations can be performed.

  5. "."

    Returns: 0

  6. "C"

    Returns: 0

  7. "R"

    Returns: 0

  8. ".."

    Returns: 0

  9. ".C"

    Returns: 1

  10. ".R"

    Returns: 0

  11. "C."

    Returns: 0

  12. "CC"

    Returns: 0

  13. "CR"

    Returns: 0

  14. "R."

    Returns: 0

  15. "RC"

    Returns: 0

  16. "RR"

    Returns: 0

  17. "RR.R..C.CC..CRRRRRC.C.C.RCC..CR....RC.C.."

    Returns: 7

  18. "RRR.R...CRCCR...C.CRR.CCCR.C.CC.C.RR.C.C.RR"

    Returns: 9

  19. "C..RR.RR..C.CRRR..RCR...RC..CRR.CC...RRRRRRC."

    Returns: 6

  20. "CRRCRC.RCRRR.CCR.C.CCCCR.R....CRRR.RCRC.CR..R.R"

    Returns: 4

  21. "...C.RCRRR..RRCCCR.CCRRC.CRRCRCR.RCRCC..RCC.RCRRR"

    Returns: 8

  22. "RC...RCRR.RRRRCC.RRRCRC.C.RRC..C.RRRR..RCRCC.RCCC."

    Returns: 7

  23. "CCRC..RC.R..RRC.RRR.CR.R.RC.RCCRC.RRR.RCCRRCCRR..R"

    Returns: 8

  24. "C..RCRR..CRR..RR.C.RCR.R.CC..CRCCRCCRCCCRCC.C.CR.R"

    Returns: 12

  25. "C.RRRCR.RRC....RR.RC..R....CR.CRR.RR.CRCC.C......R"

    Returns: 7

  26. ".RRCR.RCC..R...CCCC..CCRR.CR.CCR.C.R.RCC.CCCRR.RRR"

    Returns: 9

  27. ".........................CCCCCCCCCCCCCCCCCCCCCCCCC"

    Returns: 25

  28. "........................CCCCCCCCCCCCCCCCCCCCCCCCCC"

    Returns: 24

  29. "........................RCCCCCCCCCCCCCCCCCCCCCCCCC"

    Returns: 24

  30. "....CCCCCCCCCCCCC"

    Returns: 4

  31. "CCCCCCCCCCCCC.C"

    Returns: 1

  32. ".C"

    Returns: 1

  33. "CC"

    Returns: 0

  34. "RRRRR"

    Returns: 0

  35. "RRRRRR"

    Returns: 0

  36. "CCC"

    Returns: 0

  37. "RRRRRCRR"

    Returns: 0


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: