FastPrepClosest Rhyming Word

Closest Rhyming Word

Accenture logoAccenture● EasyINTERNOA
Learn

Problem statement

Given a target word target and an array of candidate words words, return the candidate with the largest rhyme score.

The rhyme score is the number of matching characters in the longest common suffix of target and a candidate. If several candidates have the same score, return the one that appears first in words.

Function

closestRhymingWord(target: String, words: String[]) → String

Examples

Example 1

target = "thunder"words = ["plunder","wonder","rain"]return = "plunder"

plunder and thunder share the suffix under, which is longer than every other shared suffix.

Example 2

target = "stone"words = ["alone","phone","stove"]return = "alone"

alone and phone tie with the suffix one, so the earlier candidate is returned.

Example 3

target = "cat"words = ["dog","sun","map"]return = "dog"

No candidate shares a non-empty suffix with target, so every score is 0 and the first candidate wins.

Constraints

  • 1 <= target.length <= 10^5.
  • 1 <= words.length <= 10^5.
  • Every word contains only lowercase English letters.
  • The total number of characters across words is at most 10^5.

More Accenture problems

See Accenture hiring insights
public String closestRhymingWord(String target, String[] words) {
    // Write your code here.
}
target"thunder"
words["plunder","wonder","rain"]
expected"plunder"
Checking account…