Most Relevant Text Span
Updated
Problem statement
You are given a nonempty lowercase string text and an integer array scores of the same length. The relevance of a nonempty contiguous span is the sum of the scores aligned with its characters.
Return the substring with the largest relevance. If several spans have the same largest relevance, return the one with the earliest starting index. If they also have the same starting index, return the shortest one.
Function
mostRelevantSpan(text: String, scores: int[]) → StringExamples
Example 1
text = "search"scores = [-2,4,3,-5,2,1]return = "ea"The span "ea" has relevance 4 + 3 = 7, which is the largest possible sum.
Example 2
text = "matrix"scores = [-4,-2,-7,-1,-5,-3]return = "r"Every score is negative. The single character "r" has score -1, the largest available relevance.
Example 3
text = "abcd"scores = [1,-1,1,-1]return = "a"Several spans have relevance 1. The earliest starts at index 0, and "a" is the shortest span with that start and score.
Constraints
1 ≤ text.length = scores.length ≤ 2 * 10^5.textcontains only lowercase English letters.-10^9 ≤ scores[i] ≤ 10^9.
More Hebbia interview questions
Similar questions at other companies
- Largest Number Below N from Reusable DigitsByteDance · PHONE SCREENByteDance interview questions
- Jump GamePaytm · PHONE SCREENPaytm interview questions
- Query Words Within K PositionsPalantir · PHONE SCREENPalantir interview questions
- Word BreakAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Minimum Cost for a Target WeightPace Stock Broking Services · PHONE SCREEN
- Jump Game IINavi · PHONE SCREEN
- Best Stock Profit With a Sell FeeKaseya · PHONE SCREEN
- Maximize Workday EarningsCitadel · OA · PHONE SCREEN · ONSITE INTERVIEWCitadel interview questions
- Biggest Number From Gluing Numbers TogetherAdobe · PHONE SCREENAdobe interview questions
- Return a Minimum-Coin Change BreakdownDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions