Code Question 1
A manager at Amazon is responsible for organizing a thrilling online shopping event for a tech festival, and they have come up with an exciting concept. In this game, users are presented with a sequence of digits representing a product code. The challenge is to find a new product code that is equally attractive or even more attractive than the original one.
Let’s call an integer, represented in its decimal notation in order from left to right, b[1], b[2], b[3], ..., b[n] attractive if b[i] = b[i+k] for each i such that 1 ≤ i ≤ n - k, where n
and k
are positive integers. For example:
- "252525" is attractive if
k = 2
- "245254" is not attractive.
Task:
Your task is to generate a new product code, represented by string new_code
, such that it is greater than or equal to the original product code org_code
.
Formal Definition:
Given a string org_code
of size n
representing the digits in the decimal notation and integer k
, find a string new_code
representing the digits in decimal notation, which satisfies:
old_code ≤ new_code
new_code
is attractive, given that there are no leading zeroes inold_code
andnew_code
.
Additional Details:
A string s
of size n
representing the digits in decimal notation is lexicographically less than string p
of length m
if one of the two statements is correct:
n < m
ands
is the beginning (prefix) ofp
.s[1] = p[1], s[2] = p[2], ..., s[k-1] = p[k-1]
, ands[k] < p[k]
for somek
(1 ≤ k ≤ min(n, m)).
Here, characters in strings are numbered starting from 1.
Example
Input:
org_code = "1234"
k = 2
Explanation:
Given org_code = "1234"
, where |org_code| = n = 4
, and k = 2
, the task is to find the smallest new_code
such that:
- It is greater than or equal to
org_code
, and - It satisfies the attractiveness condition (digits repeat every
k
positions).
Steps:
- Take the first
k
digits fromorg_code
("12") and repeat them to form the candidatenew_code = "1212"
. - Since "1212" is smaller than "1234", increment the first two digits, resulting in "13", and repeat it to get
new_code = "1313"
.
Output:
new_code = "1313"
This new code is attractive because:
- b[0] = b[2] = "1", and b[1] = b[3] = "3", and
- It satisfies
org_code ≤ new_code
(as "1313" ≥ "1234").
Thus, "1313" is the required new_code
.
Function Description
Complete the function findSmallestAppealing in the editor below.
findSmallestAppealing has the following parameters:
string old_code
: integerold_code
represented as a string in decimal notation.int k
: a positive integer such thatk < n
.
Returns:
string
: smallest attractive integernew_code
, such thatold_code ≤ new_code
, in decimal notation.
Constraints
- 1≤n≤2×1051 ≤ n ≤ 2 × 10^5
- 1≤k<n1 ≤ k < n
- 0≤orgcode[i]≤90 ≤ org_code[i] ≤ 9 for 0≤i<n0 ≤ i < n, and orgcode[0]≠0org_code[0] ≠ 0.
Sample Input 0
Input:
org_code = "1234"
k = 2
Output:
1313
我们长期稳定承接各大科技公司如TikTok、Google、Amazon等的OA笔试代写服务,确保满分通过。如有需求,请随时联系我们。
We consistently provide professional online assessment services for major tech companies like TikTok, Google, and Amazon, guaranteeing perfect scores. Feel free to contact us if you're interested.