FastPrepMost Relevant Text Span

Most Relevant Text Span

Hebbia logoHebbia● MediumFULLTIMEPHONE SCREEN
Learn

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[]) → String

Examples

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.
  • text contains only lowercase English letters.
  • -10^9 ≤ scores[i] ≤ 10^9.

More Hebbia problems

See Hebbia hiring insights
public String mostRelevantSpan(String text, int[] scores) {
    // Write your code here.
}
text"search"
scores[-2,4,3,-5,2,1]
expected"ea"
Checking account…