2. Question 2
For a string s
which consists only of characters '0' and '1', find the number of subsequences of length 5 which are palindromes.
As the answer can be really big, return the answer mod (10^9 + 7).
Note:
- A palindrome is a sequence that reads the same backward as forward.
- A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements.
- Two subsequences are considered different if the string that forms the subsequences are different.
Examples = "0100110"
Using 1-based indexing, the 5 subsequences are:
- Indices (1, 2, 3, 6, 7) -> "01010"
- Indices (1, 2, 3, 5, 7) -> "01010"
- Indices (1, 2, 4, 6, 7) -> "01010"
- Indices (1, 2, 4, 5, 7) -> "01010"
- Indices (1, 2, 5, 6, 7) -> "01110"
5 modulo (10^9 + 7) = 5
.
Function Description
Complete the function getPalindromesCount
in the editor below.
getPalindromesCount
has the following parameter:
- string s: the binary string
Returns
- int: the number of subsequences of length 5 which are palindromes, modulo (10^9 + 7).
Constraints:
- 1 ≤ length of
s
≤ 10^5
3. Question 3
Implement a prototype of a friend recommendation system for a social media application.
There are n
users indexed from 0
to n-1
, and m
friends represented as a 2D array, friendships
, where the i-th
friendship is a connection between users friendships[i][0]
and friendships[i][1]
.
User x
is suggested as a friend to user y
if x
and y
are not friends and have the maximum number of common friends, i.e., a friend of both x
and y
. If there are multiple possible such users x
, the one with the minimum index is suggested.
Given n
, friendships
, and y
, determine which user should be recommended to y
as a friend. If there is no recommendation available, report -1
.
Example
Suppose n = 5
, m = 5
, andconnections = [[0, 1], [0, 2], [1, 3], [2, 3], [3, 4]]
.
For user y = 4
, the recommendation is user 1
.
Function Description
Complete the function friendRecommendation
with the following parameters:
- int n: the number of users
- list[list[int]] friendships: a 2D array representing the friendships
- int y: the user to recommend a friend to
Returns
- int: the user index of the friend recommendation, or
-1
if no recommendation is available.
Constraints
- 1 ≤
n
≤ 10^5 - 1 ≤
m
≤ 2 × 10^5 - 0 ≤
friendships[i][0], friendships[i][1]
<n
friendships[i][0]
≠friendships[i][1]
我们长期稳定承接各大科技公司如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.