Correct Sensor Readings
Problem statement
A monitoring system stores sensor readings in the space-separated string sensorReadings. Each reading is a non-negative decimal integer.
Process every original reading independently:
- If it contains any pair of equal adjacent digits, replace the entire reading with the sum of all its digits.
- Otherwise, preserve the reading unchanged.
Perform at most one replacement per original reading. Do not process the replacement again, even if its digits are equal. Return the resulting readings in their original order, separated by single spaces.
Input convention
There is exactly one space between readings and no leading or trailing space. Readings have no leading zeros except for the single digit 0.
Function
correctSensorReadings(sensorReadings: String) → StringExamples
Example 1
sensorReadings = "123 4558 787"return = "123 22 787"4558 has adjacent equal digits 55, so replace the entire number by 4 + 5 + 5 + 8 = 22. Stop after that replacement; 22 is not processed again. The two 7s in 787 are not adjacent, so it stays unchanged.
Example 2
sensorReadings = "123456789"return = "123456789"No two consecutive digits are equal, so the reading remains unchanged.
Example 3
sensorReadings = "88 909 100 0"return = "16 909 1 0"88 becomes 16, 909 stays unchanged, and 100 has adjacent zeros so its complete digit sum is 1. The single digit 0 remains unchanged.
Constraints
1 <= sensorReadings.length <= 10000.- Each reading contains from
1to9decimal digits. - Readings are non-negative, have no leading zeros except
0, and are separated by exactly one ASCII space. - There is no leading or trailing space.