Code Question 1
Amazon developers are building a prototype for a regex generator for the given strings. For the prototype version, the regex consists of uppercase English letters, [
and ]
. Here, a string of characters enclosed in square brackets [
and ]
matches any of the characters in that string. Also, the string of characters enclosed within [
and ]
consists of uppercase English letters only with no repetition.
For example:
"[ABBR]ABC"
is not a valid regex asB
occurs twice between square brackets."[ABC"
,"AA[C]"
,"]T"
and"JABC"
are also not valid regexes as brackets must contain some string, and brackets should also form a balanced bracket sequence."[ABCD][CA]"
is a valid regex and matches with"BBCA"
and"ABCA"
, but not with"ABBCA"
."[ABCZ][Q]"
is a valid regex and matches with"AQ"
,"BQ"
,"CQ"
,"ZQ"
but not with"DQ"
or"ZC"
."[AB][C][D]"
is a valid regex and matches only with the string"ABCD"
.
Problem Statement
Given 3 strings x
, y
, and z
of length n
, find the longest regex that matches both the strings x
and y
but does not match with the string z
. If no such regex exists, output -1
. If multiple such regexes exist, output the lexicographically smallest one.
Notes
- The length of a regex is the number of characters in it, i.e., including uppercase English alphabets,
[
and]
. - A string
p
is lexicographically smaller than stringq
ifp
is a prefix ofq
, is not equal toq
or there existsj
such thatp[j] < q[j]
and for alli < j
,p[i] = q[i]
. Note that while comparingp[j]
andq[j]
, we consider their ASCII values, i.e.,[
and]
are greater than uppercase English letters. For example:"ABC"
is lexicographically smaller than"BCD"
."ABCDE"
but larger than"AACD"
and"AACDE"
.
- The answer string, which is to be returned, can turn out to be in the order of 10^6 for larger
n
.
Example
Input:
x = "AB"
y = "BD"
z = "CD"
Output:
[ABCDEFGHJKLMNOPQRSTUVWXYZ][ABCDEFGHJKLMNOPQRSTUVWXYZ]
Explanation:
Regex [ABCDEFGHJKLMNOPQRSTUVWXYZ][ABCDEFGHJKLMNOPQRSTUVWXYZ]
matches with strings "AB"
and "BD"
, but not with string "CD"
.
Input:
x = "ABCD"
y = "CODE"
z = "CODE"
Output:
-1
Explanation:
As strings y
and z
are equal, it is not possible that a regex matches with y
but not with z
. Hence, the output is -1
.
Function Description
Complete the function findLongestRegex
in the editor below.
Parameters:
x
: a stringy
: a stringz
: a string
Returns:
string
: the longest lexicographically smallest regex matchingx
andy
but notz
. If no such regex exists, return-1
as a string.
Constraints
1 ≤ n ≤ 10^5
x
,y
, andz
consist of uppercase English letters only.
Code Question 2
A team of financial analysts at Amazon closely monitors revenue generated by a newly launched product. They classify a period of one or more consecutive days as a stable-growth period if the revenue generated by the product takes no more than k
distinct values over that period.
Given an array revenues
of size n
that represents the revenues generated by the new product on n
consecutive days, and an integer k
, determine the total number of stable growth periods over the n
days. Since the answer can be large, return it modulo (10^9 + 7).
Example
Suppose n = 3
, k = 1
, revenues = [1, 2, 1]
.
Indices of days | Periods | Distinct Values |
---|---|---|
[0] | [1] | 1 |
[0, 1] | [1, 2] | 2 |
[0, 1, 2] | [1, 2, 1] | 2 |
[1] | [2] | 1 |
[1, 2] | [2, 1] | 2 |
[2] | [1] | 1 |
There are three periods with k = 1
or fewer distinct values. The number of stable growth periods is 3.
Function Description
Complete the getStablePeriodsCount
function in the editor below.
Parameters:
int revenues[n]
: the revenues generated by the new product overn
daysint k
: the maximum number of distinct values in a stable growth period
Returns:
int
: the number of stable growth periods of the product overn
days, modulo (10^9 + 7)
Constraints
1 ≤ n ≤ 10^5
1 ≤ k ≤ n
-10^9 ≤ revenues[i] ≤ 10^9
Example Input and Output
Sample Input 0:
n = 4
revenues = [2, -3, 2, -3]
k = 2
Sample Output 0:
10
Explanation:
Any contiguous period of 1 or more days has 2 or fewer distinct values, thus all 10 subarrays represent a period of stable growth.
Sample Input 1:
n = 6
revenues = [2, 3, -4, -1, -2, -1]
k = 2
Sample Output 1:
<Output not provided in image>
我们长期稳定承接各大科技公司如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.