IntegerGenerator
SRM 281 · 2006-01-05 · by lovro
Problem Statement
As part of a larger scale project, you need to write a component which generates consecutive positive integers. Only certain digits may appear in the input and in the integers generated, and leading zeros aren't allowed.
You are given a
If current represents an invalid integer according to the first paragraph, return "INVALID INPUT" (quotes for clarity).
Constraints
- allowed will contain between 0 and 10 elements, inclusive.
- Each element in allowed will be between 0 and 9, inclusive.
- allowed will contain no duplicates.
- current will contain between 1 and 10 digits ('0'-'9'), inclusive.
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }
"16"
Returns: "17"
With all digits available, the next number is 17.
{ 0, 1, 2, 3, 4, 5, 6, 8, 9 }
"16"
Returns: "18"
The digit 7 is no longer allowed, so the next smallest valid integer is 18.
{ 3, 5, 8 }
"548"
Returns: "INVALID INPUT"
The current number may not contain disallowed digits.
{ 5, 3, 4 }
"033"
Returns: "INVALID INPUT"
Leading zeros aren't allowed either.
{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }
"999"
Returns: "1000"
{ 0, 1, 2, 3, 4, 5 }
"0"
Returns: "INVALID INPUT"
The generator only works with positive integers.
Submissions are judged against all 150 archived test cases, of which 6 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class IntegerGenerator with a public method string nextInteger(vector<int> allowed, string current) · 150 test cases · 2 s / 256 MB per case