In Amazon's distributed storage network, some critical data segments are missing. They are represented by a string missingData. The system restores data by choosing a base segment of length segmentSize and repeatedly appending copies of that base segment to a generated string.
A base segment is valid if, after some number of replications, the generated string contains every character in missingData at least as many times as it appears in missingData.
Among all valid base segments, choose one that requires the fewest replications. If more than one base segment requires that same minimum number of replications, return the lexicographically smallest one. If no valid base segment exists, return "-1".
Complete the function getSmallestBaseSegment.
getSmallestBaseSegment has the following parameters:
int segmentSize: the length of the base segment that can be replicatedString missingData: a string representing the missing data segments
Returns
String: the lexicographically smallest valid base segment of length segmentSize that minimizes the required number of replications, or "-1" if no such segment exists.
segmentSize = 2 missingData = "aavvavv" return = "av"
The character a appears 3 times and v appears 4 times. Both "av" and "va" require 4 replications. Since "av" is lexicographically smaller, it is returned.
segmentSize = 1 missingData = "abc" return = "-1"
A base segment of length 1 can contain only one distinct character, so it cannot generate all three required characters.
1 <= segmentSize <= missingData.lengthmissingDataconsists of lowercase English letters.
- Count Promotional PeriodsOA · Seen Jun 2026
- Find Maximum Total Amount (SDE I, Fungible :)Seen Jun 2026
- Get Minimum AmountOA · Seen Jun 2026
- Find Minimum CostOA · Seen Jun 2026
- Select Least Resource TasksOA · Seen Jun 2026
- Product Category Group SizesPHONE SCREEN · Seen May 2026
- Count Connected ComponentsPHONE SCREEN · Seen May 2026
- Drone Delivery RouteOA · Seen May 2026
public String getSmallestBaseSegment(int segmentSize, String missingData) {
// write your code here
}